编程语言
首页 > 编程语言> > python – NDB:如何将get_by_id用于实体组中的实体

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

作者:互联网

我正在使用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的父母的文件夹?

解决方法:

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

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

标签:python,google-app-engine,app-engine-ndb
来源: https://codeday.me/bug/20190613/1232907.html