【发布时间】:2016-02-13 23:46:43
【问题描述】:
我有一种情况,我使用的模型具有所有实体所需的许多属性和一些特定于两个替代方案之一的属性,如下例所示:
class Machine(ndb.Model):
#Properties for all machines:
price = ndb.FloatProperty()
model = ndb.StringProperty()
vendor = ndb.KeyProperty()
#...
#Properties exclusive to type A machines:
foo1 = ndb.StringProperty()
foo2 = ndb.StringProperty()
#Properties exclusive to type B machines:
bar1 = ndb.StringProperty()
bar2 = ndb.StringProperty()
据我了解,我可以选择两条路径之一。要么我把它变成一个普通的 ndb.Model 并且只留下一些没有内容的属性,或者我可以使用一个 ndb.Expando 模型并提前设置固定的属性,然后根据情况添加其他属性。
据我了解,Expando 模型在您不知道需要的属性时很有用,但我事先知道所需的所有属性(foo1、foo2、bar1、bar2)。我应该使用 Expando 还是可以使用常规 ndb.Model 并将每个实体的一些属性留空?
【问题讨论】:
标签: google-app-engine google-cloud-datastore app-engine-ndb