gpt4 book ai didi

google-app-engine - get_by_id() 不会返回模型实例

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:40 24 4
gpt4 key购买 nike

我有一个名为 Version 的模型,如下所示:

from google.appengine.ext import db
import piece

class Version(db.Model):
"A particular version of a piece of writing."

parent_piece = db.ReferenceProperty(piece.Piece, collection_name='versions')
"The Piece to which this version belongs."

note = db.TextProperty()
"A note from the Author about this version."

content = db.TextProperty()
"The actual content of this version of the Piece."

published_on = db.DateProperty(auto_now_add=True)
"The date on which the version was published."

我想使用 Version.get_by_id() 通过 ID 访问 Version 的实例,但此调用总是返回 None。我可以在 Datastore Viewer 中看到它们具有 ID 值,在调试器中,我可以查询它们但不能使用它们:

>>> for each_ver in version.Version.all():
... print each_ver.key().id()
...
34
35
36
31
32
>>> a = version.Version.get_by_id(34)
>>> type(a)
<type 'NoneType'>

我看到这里有很多问题,人们可以按照我的意愿有效地使用 get_by_id(),但他们看不到我看到的结果。

问题可能是每个 Version 实例都是实体组中的子项而不是实体组的根吗?每个 Version 都位于一个看起来像 Member->Piece->Version 的实体组中。如果这是问题所在,有没有一种方法可以在不使用其整个 key 的情况下引用 Version 实体?如果这不是问题,谁能告诉我如何使 get_by_id() 按预期工作?

最佳答案

Could the problem be that each Version instance is a child in an Entity Group rather than a root of an Entity Group?

是的。实体的键包括任何父实体的键。

If that is the problem, is there a way that I can refer to Version entity without using its entire key?

没有。实体仅由其整个键唯一标识,其中包括所有父实体的键。不过,如果您知道其父实体的种类,则可以使用 db.Key.from_path 从 ID 链或 key 名称构造 key 。

关于google-app-engine - get_by_id() 不会返回模型实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3072939/

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