看起来KeyProperty的kind
验证不适用于PolyModel
的子类。
from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel
class Item(polymodel.PolyModel):
parent = ndb.KeyProperty(kind="Folder")
class Folder(Item):
title = ndb.StringProperty()
def add_item(self, item):
item.set_parent(self.key)
class File(Item):
pass
class Main(webapp2.RequestHandler):
def get(self):
rootfolder = Folder(title="root")
rootfolder.put()
# the next line raise exception
subfolder = Folder(title="Cool things", parent=rootfolder.key)
subfolder.put()
例外:
line 1935, in _validate
'Expected Key with kind=%r, got %r' % (self._kind, value))
BadValueError: Expected Key with kind='Folder', got Key('Item', 6544293208522752)
看起来和圭多·范·罗苏姆说的一样可以。键属性在使用模型继承时引用基模型类?
它实际上按预期工作。
您只能具有parent = ndb.KeyProperty(kind="Item")
,因为数据存储中存储的文件夹类型为项目。
它具有定义其继承继承制的其他属性,并允许您执行Item.query()等查询并获取Item的所有子类。
再次阅读PolyModel文档并查看数据存储中存储的实体,然后一切都会清楚。