gpt4 book ai didi

python - 如何在 django 模板中获取引用属性

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:23 25 4
gpt4 key购买 nike

我在 Google 应用引擎上使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com