【发布时间】:2016-12-28 21:40:58
【问题描述】:
我正在寻找存储具有共同父级的不同种类实体列表的最佳方式。我想出了以下几点:
class Action(ndb.Model):
date = ndb.DateTimeProperty(auto_now_add = True)
class ContentAction(Action):
content = ndb.StringProperty()
class AuthoredAction(Action):
author = ndb.StringProperty()
class ActionContainer(ndb.Model):
actions = ndb.StructuredProperty(Action, repeated=True, indexed=False)
不幸的是,一旦我将 2 个不同类型的实体添加到动作列表中,就无法判断该实体是哪种类型(它们都是动作)。
此外,与作为第一个添加的实体不同的类的实体的继承属性似乎丢失了。例如,在添加 ContentAction 和 AuthoredAction 之后,后一个对象是没有“author”属性的 Action。也许 StructuredProperty 只允许存储相同类型的对象?有没有更好的结构方式?
【问题讨论】:
标签: python google-app-engine google-cloud-datastore app-engine-ndb