4

我正在使用 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

是设计使然还是错误?我看到以前使用命名空间时出现了一些与 get_by_id() 相关的问题(我使用的是 SDK 1.6.6)。我怎样才能文件夹有来自 id 的父母?

4

1 回答 1

8

get_by_id(id, parent=None)接受一个父参数。
当您在实体组中按 id 查询时,您必须包含父键才能获得所需的实体。

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

于 2012-06-15T13:11:21.307 回答