【问题标题】:Is there a way to save nested entities in gcloud-python?有没有办法在 gcloud-python 中保存嵌套实体?
【发布时间】:2016-06-27 11:07:33
【问题描述】:

我正在尝试将一个对象保存到 Cloud Datastore,该对象包含一个字典作为属性值:

client = datastore.Client(project_id)
key = client.key('Config', 'config', 'Environment', 'env_name')
env = datastore.entity.Entity(key)
env['prop1'] = dict(foo='bar')
client.put(env)

但它提高了

ValueError: Unknown protobuf attr type

虽然我可以使用 gcloud-node 做到这一点。

是否可以使用 gcloud-python 保存复合对象?

【问题讨论】:

    标签: google-cloud-datastore gcloud-python google-cloud-python


    【解决方案1】:

    听起来您对存储嵌入式实体感兴趣,我相信这是 gcloud-node 自动执行的操作。

    我认为您可以通过将字段 (prop1) 设置为包含子属性 (foo) 设置为 'bar'datastore.Entity 来做到这一点。

    client = datastore.Client(project_id)
    key = client.key('Config', 'config', 'Environment', 'env_name')
    env = datastore.Entity(key)
    env['prop1'] = datastore.Entity(key=client.key('EmbeddedKind')
    env['prop1']['foo'] = 'bar'
    client.put(env)
    

    当你把它拿回来时,它看起来像......

    >>> c.get(env.key)
    <Entity[{'kind': u'Config', 'name': u'config'}, {'kind': u'Env', 'name': u'env_name'}] {u'prop1': <Entity[{'kind': u'Embedded'}] {u'foo': 'bar'}>}>
    

    【讨论】:

    • 感谢您的回复!当我使用 gcloud-node 创建子实体时,它不会创建新类型,为什么它是 gcloud-python 中的唯一方法?最好避免像 gcloud-node 那样为嵌入式实体创建一种新类型。
    • 我同意 - 有兴趣针对 gcloud-python 提出问题,以便那边的人可以开始工作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多