我有一个名为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 。我可以在数据存储区查看器中看到它们具有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(),但他们看不到我看到的结果。
问题可能是每个版本实例都是实体组中的子实体而不是实体组的根?每个版本都存在于一个看似Member-&gt; Piece-&gt;版本的实体组中。如果这是问题,有没有办法可以在不使用整个密钥的情况下引用Version实体?如果这不是问题,有人可以告诉我我可以做些什么来使 get_by_id()按预期工作?
答案 0 :(得分:6)
问题可能是每个版本 instance是实体组中的子项 而不是实体组的根?
是。实体的密钥包括任何父实体的密钥。
如果那是问题,那就有了 我可以参考版本实体的方式 没有使用它的全部密钥?
没有。实体仅由其整个密钥唯一标识,其中包括所有父实体的密钥。但是,如果您知道其父实体的种类,则可以使用db.Key.from_path从ID或键名链构造密钥。
答案 1 :(得分:0)
我有同样的问题,但在ndb.Model中,我发现我需要将ID转换为int。因此,使用version.Version.get_by_id(int(34))
可以解决您的问题。