如何在Google App Engine的db.ListProperty中引用某个模型

0 投票
1 回答
626 浏览
提问于 2025-04-16 01:27

这是我的模型:

class Geo(db.Model):
    entry = db.ListProperty(db.Key)

geo=Geo()
geo.entry.append(otherModel.key())

而HTML代码是:

{% for i in geo.entry %}
        <p><a href="{{ i.link }}">{{ i.title }}</a></p>
{% endfor%}

但是它什么都不显示,

我觉得可能应该:

class Geo(db.Model):
    entry = db.ListProperty(db.Model)
geo=Geo()
geo.entry.append(otherModel)

但是它显示的是:

ValueError: Item type Model is not acceptable

所以,怎样才能让HTML显示正确的内容呢。

谢谢

1 个回答

1

你不能直接用那个模板(或者类似的东西)来展示模型,但你可以很简单地准备一个包含模型列表的上下文,只需要在键的列表上调用 db.get 就可以了。比如,你可以在上下文字典的开头写 {'entries': db.get(listofkeys), ...,然后在模板中用 for i in entries 来遍历这些条目。

撰写回答