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

标签 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 暂时在马里廷巴克图市工作。我该如何检索和更新他的工作地址?

谢谢。

最佳答案

我会尝试这样的事情。

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

guido.put()

编辑 添加了一个冒号

关于python - 如何检索和操作 ndb StructuredProperty 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12446132/

相关文章:

google-cloud-datastore - 从 PubsubIO 读取写入 DatastoreIO

python - 基于最大编辑距离的最可能的单词

python - 在 centos 6.5 上安装 tensorflow

python - 将 Tkinter slider 链接到文本输入

python - Django:导入错误:无法导入名称 'User'

google-app-engine - 在 App Engine 上使用 pem 文件的安全方式

java - 无法运行 Maven Google 标准应用程序引擎 1.9.68 原型(prototype)

java - 如何修改此 Mapreduce 代码以更改实体的命名空间?

java - GQL 查询问题,Google 数据存储。多个条件以及大于和小于运算符的错误

python - 将数据从 Google Datastore 加载到 Google Cloud ML Engine