get_by_id()不会返回模型实例 [英] get_by_id() will not return model instance

查看:121
本文介绍了get_by_id()不会返回模型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  from google.appengine.ext import db 
进口件

类版本(db.Model):
一段文字的特定版本。

parent_piece = db.ReferenceProperty(piece.Piece,collection_name ='versions')
该版本所属的部分。

note = db.TextProperty()
作者关于此版本的说明。

content = db.TextProperty()
该版本的实际内容。

published_on = db.DateProperty(auto_now_add = True)
发布版本的日期。

我想通过它们的ID使用Version.get_by_id()来访问Version的实例,但是这个调用始终返回 None 。我可以在Datastore Viewer中看到它们具有ID值,在调试器中,我可以查询它们但不使用它们: C>>>> ():
...打印each_ver.key().id()
...
34
35
36
31
32
>>> a = version.Version.get_by_id(34)
>>>类型(a)
< type'NoneType'>

我发现在这里有很多问题,人们能够有效地使用get_by_id()我希望,而且他们看不到我看到的结果。



问题可能是每个版本实例是实体组而不是实体组的根?每个版本都位于类似于成员 - >件 - >版本的实体组中。如果这是问题,有没有一种方法可以在不使用整个密钥的情况下引用版本实体?如果这不是问题,谁能告诉我可以做些什么来使 get_by_id()按预期工作?

/ div>


难道问题是每个版本
实例是一个实体组
中的子元素,而不是实体组的根元素吗?


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


如果这是问题,是否有
的方式可以请参阅版本实体
,而不使用它的整个密钥?

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


I have a Model called Version that looks like this:

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."

I would like to access instances of Version via their IDs, using Version.get_by_id(), but this call always returns None. I can see in the Datastore Viewer that they have ID values, and in the debugger, I can query for them but not use them:

>>> 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'>

I see that there are plenty of questions here where people are able to use get_by_id() effectively just as I wish, and they do not see the results that I am seeing.

Could the problem be that each Version instance is a child in an Entity Group rather than a root of an Entity Group? Each Version lives in an Entity Group that looks like Member->Piece->Version. If that is the problem, is there a way that I can refer to Version entity without using its entire key? If that is not the problem, can anyone tell me what I can do to make get_by_id() work as expected?

解决方案

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

Yes. An entity's key includes the keys of any parent entities.

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

No. An entity is uniquely identified only by its entire key, which includes the keys of all the parent entities. If you know the kinds of its parent entities, though, you can use db.Key.from_path to construct the key from the chain of IDs or key names.

这篇关于get_by_id()不会返回模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆