简体   繁体   English

ndb通过查询StructuredProperty返回一个StructuredProperty子属性

[英]ndb returning a StructuredProperty subproperty by querying for a StructuredProperty

Hi I'm confused about how to return a StructuredProperty property (mouthful): 嗨,我对如何返回StructuredProperty属性(含糊)感到困惑:

Say I have this example from the ndb tutorial: 假设我有来自ndb教程的以下示例:

class Address(ndb.Model):
  type = ndb.StringProperty() # E.g., 'home', 'work'
  street = ndb.StringProperty()
  city = ndb.StringProperty()

class Contact(ndb.Model):
  name = ndb.StringProperty()
  addresses = ndb.StructuredProperty(Address, repeated=True)

guido = Contact(name='Guido',
                addresses=[Address(type='home',
                                   city='Amsterdam'),
                           Address(type='work',
                                   street='Spear St',
                                   city='SF')])

guido.put()

I want to be able to query for city Amsterdam and have it return the type "home". 我希望能够查询阿姆斯特丹市,并使其返回类型“ home”。

so if I did the query: 因此,如果我执行查询:

Contact.query(Contact.address == Address(city='Amsterdam'))

I would want it to return Home. 我希望它返回首页。

Queries in appengine by default return the whole entity. 默认情况下,appengine中的查询会返回整个实体。 If I understand you correctly, you just want a field of the structured property returned, rather than the whole entity. 如果我对您的理解正确,则只需要返回结构化属性的字段,而不是整个实体。

If this is the case, the you want to read up on projection queries. 在这种情况下,您想阅读投影查询。

https://developers.google.com/appengine/docs/python/ndb/queries#projection https://developers.google.com/appengine/docs/python/ndb/queries#projection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM