[英]GAE NDB with Endpoints Proto Datastore: filter by ID of reference property
I created a model called Building
with reference to a model called Office
. 我参考称为
Office
的模型创建了一个名为Building
的模型。 I would like to filter Building
s by OfficeID
in Proto REST Query ( @Building.query_method
....) 我想在Proto REST查询(
@Building.query_method
....)中按OfficeID
过滤Building
。
Currently, I work with office_key
property (need to enter Entity Key of Office), but I would like to filter by OfficeID
property. 目前,我使用
office_key
属性(需要输入Office的实体密钥),但是我想按OfficeID
属性进行过滤。 Any ideas on how to do this? 有关如何执行此操作的任何想法?
Here's what I've tried so far: 到目前为止,这是我尝试过的方法:
class Building(EndpointsModel):
_message_fields_schema = ('id', 'name', 'office')
name = ndb.StringProperty(default=None, indexed=True)
office_key = ndb.KeyProperty(kind=Office, required=False)
def office_setter(self, value):
self.office_key = ndb.Key('Office', value.id)
@EndpointsAliasProperty(setter=office_setter, property_type=Office.ProtoModel())
def office(self):
return self.office_key.get()
class Office(EndpointsModel):
_message_fields_schema = ('id', 'name', 'created_date')
name = ndb.StringProperty(default=None, indexed=True)
created_date = ndb.DateTimeProperty(auto_now_add=True)
@Building.query_method(query_fields=('limit', 'order', 'pageToken', 'office_key'), path='buildings', name='list')
def List(self, query):
return query
It appears that Building -> Office is a one to one relationship (conversely the relationship of office -> building is many to one. 似乎建筑物->办公室是一对一的关系(相反,办公室->建筑物之间的关系是多对一的。
So you need to store the office id in the building as a cached property for querying. 因此,您需要将办公室ID作为查询的缓存属性存储在建筑物中。
Then you can query for Buildings with a specific office id. 然后,您可以查询具有特定办公室ID的建筑物。
Alternately query for all buildings that the key held in office_key == 'some office key') 交替查询在office_key =='some office key'中保存的所有建筑物)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.