NDB:如何将get_by_id用于实体组中的实体

时间:2012-06-15 12:53:23

标签: python google-app-engine app-engine-ndb

我正在使用get_by_id()来读取NDB中的实体,我不认为它适用于属于实体组的实体:

from google.appengine.ext import ndb

class Folder(ndb.Model):
    name = ndb.StringProperty()

parent_folder_key = Folder(name = 'Parent folder').put()    
sub_folder_key = Folder(name = 'Subfolder', parent=parent_folder_key).put() 

id_list = []
print 'All folders:'
for f in Folder.query():
  print f
  id_list.append(f.key.id())

print '\nFolders by id:'
for id in id_list:
  print Folder.get_by_id(id)

输出:

All folders:
Folder(key=Key('Folder', 5814), name=u'Parent folder')
Folder(key=Key('Folder', 5814, 'Folder', 5815), name=u'Subfolder')

Folders by id:
Folder(key=Key('Folder', 5814), name=u'Parent folder')
None

是设计还是错误?我之前使用过命名空间时看到有been some issues与get_by_id()有关(我使用的是SDK 1.6.6)。我怎样才能拥有来自id的父母的文件夹?

1 个答案:

答案 0 :(得分:7)

get_by_id(id, parent=None)采用父参数  当您通过实体组中的id进行查询时,您必须包含父键才能获得所需的实体。

https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_by_id