[英]GAE NDB with Endpoints Proto Datastore: How to format response fields of reference property?
I have parent-child relationships in DataStore model: Building
entity with reference entity to Office
. 我在DataStore模型中具有父子关系:使用
Office
引用实体Building
实体。 I perform query on Building
model and I would like to limit fields of Office
entity in JSON response. 我对
Building
模型执行查询,我想在JSON响应中限制Office
实体的字段。 Here is my code: 这是我的代码:
@Building.query_method(collection_fields=('id', 'name', 'office'), path='buildings', name='list')
def List(self, query):
return query
collection_fields
attribute works great only to define parent entity fields (Building), but how to limit fields of child entity? collection_fields
属性仅适用于定义父实体字段(建筑物),但是如何限制子实体的字段呢?
Here is my response message in JSON: 这是我在JSON中的响应消息:
{ id : 5
name : 'building name'
office: {
name: 'office name',
field1 : 'test',
field1 : 'test',
field1 : 'test'
}
}
I would like to remove some fields from Office
object (ie field1,field2 etc) to reduce JSON response size. 我想从
Office
对象中删除一些字段(例如field1,field2等)以减少JSON响应的大小。 Define limited _message_fields_schema
of Office
object is not good solution, because it works globally. 定义有限的
_message_fields_schema
对Office
对象不是一个好的解决方案,因为它在全局范围内起作用。 I would like to format only this single query. 我只想格式化单个查询。
You can create EndpointsAliasProperty
in the Building
model, where you can transform self.office
and use that value in collection_fields
: 您可以在
Building
模型中创建EndpointsAliasProperty
,在其中可以转换self.office
并在collection_fields
使用该值:
@EndpointsAliasProperty
def office_ltd(self):
limited = doSomethingWith(self.office)
return limited
@Building.query_method(collection_fields=('id', 'name', 'office_ltd'),
path='buildings', name='list')
def List(self, query):
return query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.