如何在ndb中查询所有属性并包含keyproperty值?

时间:2015-02-13 10:54:25

标签: python google-app-engine google-cloud-datastore google-cloud-endpoints app-engine-ndb

本周开始使用带有Python端点的Google App Engine。 目标是查询实体并包含所有keyproperty值。 模特:

class Person(EndpointsModel):
  _message_fields_schema = ('id', 'surname', 'lastname', 'created')
  surname = ndb.StringProperty(indexed=False)
  lastname = ndb.StringProperty(indexed=False)

class Magazine(EndpointsModel):
  _message_fields_schema = ('id', 'name')
  name = ndb.StringProperty(indexed=False)

class Subscription(EndpointsModel):
  _message_fields_schema = ('id', 'publication', 'person', 'amount')
  magazine = ndb.KeyProperty(kind=Magazine)
  person = ndb.KeyProperty(kind=Person)
  amount = ndb.IntegerProperty()

以下查询这些模型以检索所有项目:

@endpoints.api(name='subscription', version='v1', description='REST API for Subscriptions')
 class SubscriptionV1(remote.Service):
   @Subscription.query_method(path='subscriptions', name='subscriptions.list')
   def SubscriptionList(self, query):
     return query

结果是:

{
 "items": [
  {
   "created": "2015-02-13T09:40:22.514000",
   "id": "6225984592281600",
   "modified": "2015-02-13T09:40:22.514000",
   "magazine": "ahNkZXZ-YXZpYW4tc2xpY2UtODUwchgLEgtQdWJsaWNhdGlvbhiAgICAgND7CQw",
   "synced": false
  }
 ]
}

虽然我希望它是:

"magazine": {"id": 123456789123456789,
"name": "Tech magazine"}

到处看,但无法找到好的查询。 查询应如何获得此结果?

1 个答案:

答案 0 :(得分:1)

如果我正确阅读您的代码,请使用提供EndpointsAliasProperty的端点proto数据存储区。在您的示例中,您可以使用它来获取正确的订阅: 将magazine重命名为magazine_key并在模型中定义EndpointsAliasProperty

@EndpointsAliasProperty(property_type=Subscription.ProtoModel())
def magazine(self):
    if self.magazine_key != None:
        magazine = self.magazine_key.get()
        return magazine
相关问题