如果您不需要查询数据中的属性,您可以使用@voscausa 提到的属性之一:
JsonProperty
class MyModel(ndb.Model):
city = ndb.StringProperty()
date = ndb.DateProperty()
data = ndb.JsonProperty()
my_model = MyModel(city="somewhere",
date=datetime.date.today(),
data={'keyword1': 3,
'keyword2': 5,
'keyword3': 1,})
结构化属性:
class Data(ndb.Model):
keyword = ndb.StringProperty()
count = ndb.IntegerProperty()
class MyModel(ndb.Model):
city = ndb.StringProperty()
date = ndb.DateProperty()
data = ndb.StructuredProperty(Data, repeated=True)
my_model = MyModel(city="somewhere",
date=datetime.date.today(),
data=[Data(keyword="keyword1", count=3),
Data(keyword="keyword2", count=5),
Data(keyword="keyword3", count=1)])
my_model.put()
这里的问题是过滤结构化属性。Keyword 的属性被视为并行数组。执行查询,例如:
q = MyModel.query(MyModel.data.keyword=='keyword1',
MyModel.data.count > 4)
会错误地包括my_model
.
https://developers.google.com/appengine/docs/python/ndb/queries#filtering_structured_properties
使用 expando 模型可以工作,并允许您查询关键字:
class MyModel(ndb.Expando):
city = ndb.StringProperty()
date = ndb.DateProperty()
m = MyModel(city="Somewhere", date=datetime.date.today())
m.keyword1 = 3
m.keyword2 = 5
m.keyword3 = 1
m.put()
q = MyModel.query(ndb.GenericProperty('keyword1') > 2)
https://developers.google.com/appengine/docs/python/ndb/entities#expando