get_by_id()不会返回模型实例

Ada*_*and 3 google-app-engine google-cloud-datastore

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

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."
Run Code Online (Sandbox Code Playgroud)

我想使用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'>
Run Code Online (Sandbox Code Playgroud)

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

问题可能是每个Version实例都是实体组中的子实体而不是实体组的根?每个版本都存在于一个看似Member-> Piece-> Version的实体组中.如果这是问题,有没有办法可以在不使用整个密钥的情况下引用Version实体?如果那不是问题,那么有人可以告诉我我可以做些什么来使get_by_id()按预期工作吗?

Nic*_*son 6

问题可能是每个Version实例都是实体组中的子实体而不是实体组的根?

是.实体的密钥包括任何父实体的密钥.

如果这是问题,有没有办法可以在不使用整个密钥的情况下引用Version实体?

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