如何检索和操作ndb StructuredProperty对象?

时间:2012-09-16 10:20:08

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

以下是StructuredProperty from the docs的示例:

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()

想象一下,Guido暂时在马里的廷巴克图市工作。我如何才能检索和更新他的工作地址?

感谢。

1 个答案:

答案 0 :(得分:3)

我会尝试这样的事情。

for address in guido.addresses:
    if address.type == 'work':
        address.street = "Main Street"
        address.city = "Timbuktu"

guido.put()

修改 添加了一个冒号