简体   繁体   English

具有Endpoints Proto数据存储区的GAE NDB:如何格式化参考属性的响应字段?

[英]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_schemaOffice对象不是一个好的解决方案,因为它在全局范围内起作用。 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.

相关问题 具有Endpoints Proto数据存储区的GAE NDB:按参考属性的ID进行过滤 - GAE NDB with Endpoints Proto Datastore: filter by ID of reference property endpoints-proto-datastore:如何忽略EndpointsAliasProperty设置程序中的必填字段? - endpoints-proto-datastore: How to ignore required fields in EndpointsAliasProperty setters? GAE上的RESTful API:端点到原始数据存储区vs云端点 - RESTful API on GAE: endpoints-proto-datastore vs Cloud Endpoints ImportError:没有名为endpoints_proto_datastore.ndb的模块 - ImportError: No module named endpoints_proto_datastore.ndb 在GAE Ndb数据存储中处理重复属性时,多余的_BaseValue() - Extraneous _BaseValue() when handling repeated property in GAE Ndb Datastore 如何通过具有终结点原型数据存储区的entityKey检索对象? - How to retrieve object by entityKey with endpoints-proto-datastore? GAE将字典转换为NDB数据存储实体 - GAE converting dictionary to NDB datastore entity 通过5种不同方式访问GAE Datastore ndb模型 - GAE Datastore ndb models accessed in 5 different ways 从GAE中的NDB数据存储中获取第一条记录 - Fetching first record from NDB datastore in GAE 有没有办法保护Google云端点原型数据存储? - Is there a way to secure Google cloud endpoints proto datastore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM