另一个StructuredProperty中的StructuredProperty.如何?

mom*_*ari 3 python google-app-engine google-cloud-datastore

我有一个实体,其中包含可变数量的另一个实体(所以我使用的是结构化属性,重复= True),但是这个属性也可以包含可变数量的单个实体类型.所以我的代码看起来像这样:

class Property(ndb.Model):
    name    = ndb.StringProperty()
    cost    = ndb.FloatProperty()
    type    = ndb.StringProperty()

class SpecialProperty(ndb.Model):
    name       = ndb.StringProperty()
    properties = ndb.StructuredProperty(Property, repeated=True)
    type       = ndb.StringProperty() 

class Hotel(ndb.Model):
    specialProperties  = ndb.StructuredProperty(SpecialProperty, repeated=True)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这个GAE时会抛出一个错误. "TypeError:此StructuredProperty不能使用repeated = True,因为它的模型类(SpecialProperty)包含重复的属性(直接或间接)."

那我怎么能绕过这个呢?我真的需要这种灵活的结构.

非常感谢提前.

Dmy*_*hyi 7

虽然可以重复StructuredProperty并且StructuredProperty可以包含另一个StructuredProperty,但要注意:如果一个结构化属性包含另一个,则只能重复其中一个.解决方法是使用LocalStructuredProperty,它没有此约束(但不允许查询其属性值).

https://developers.google.com/appengine/docs/python/ndb/properties#structured

使用LocalStructuredProperty,您将具有相同的结构,但您将无法通过此属性进行筛选.如果您确实需要通过其中一个属性进行查询 - 请尝试将其放入另一个实体.