0
看起来KeyProperty的kind
验证不适用于PolyModel
的子类。polymodel看起来不适用于KeyProperty
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)
看起来像同样的事情,吉多·范罗苏姆在 Can ndb.KeyProperty reference a base model class when using model inheritance?
我认为'kind'仅用于验证,所以它如何存储在数据存储区应该是问题? – lucemia
除了在放置期间执行验证。此时您可能会发现PolyModel的内部转换已经发生。也看看代码,看看它实际验证。如果你想限制属性到一个'文件夹'然后编写你自己的验证功能。 –
也重新验证验证错误。 “Key”不是被验证的,而是基于你的类的'Folder'对象的'Key'是'Key('Item',6544293208522752)'并且从'Key'我们可以看到类型是'Item '不'文件夹'。 –