我在Google App Engine上使用AEP

class Link():
     bag  = db.Referencepropery(Bag) #bag have name, id and other property
     name = db.Stringpropery


object_query = Link.all();
p = paginator( object_query)
object_list = p.page(1);
prefetch_references( object_list.object_list, 'bag')

render_to_response(...,{'object_list':object_list.object_list},...)


#template
{% for object in object_list%}
 {{object.bag.id}} <!--failed to get any value, why???/-->
{% end %}

最佳答案

您的代码有几处错误:


您的模型类需要扩展db.Model
您的属性需要调用构造函数,而不仅仅是引用它
StringProperty不是引用,因此“ prefetch_reference(objectlist,'name')”没有任何意义。


这是更有意义的一种:

class Link(db.Model):
     bag = db.ReferenceProperty(Bag) #bag have name, id and other property
     name = db.StringPropery()

def view():
     objectlist = get_object_list(.....)
     prefetch_reference(objectlist,'name')

关于python - 如何在Django模板中获取引用属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1332290/

10-12 21:46