如何保存没有密钥的实体?还是带有自动增量ID?

时间:2018-11-14 22:39:54

标签: google-cloud-datastore

我正在尝试使用以下方法保存实体:

import requests

from google.cloud import datastore

def saveData(x):
    client = datastore.Client()
    entity = datastore.Entity()
    entity.update({'request': x})
    client.put(entity)

if __name__ == '__main__':
    #obj = lambda: None
    #obj.data = lambda: "abcxyz"
    #processRequest(obj)
    saveData([123])

但是出现错误:ValueError: Entity must have a key

如何不用钥匙保存此内容?

根据 https://github.com/googleapis/google-cloud-python/issues/471 应该有可能吗?

以及: https://cloud.google.com/appengine/docs/standard/python/ndb/creating-entity-keys(但是这里使用的是类?)

1 个答案:

答案 0 :(得分:1)

import requests

from google.cloud import datastore

def saveData(x):
    client = datastore.Client()
    key = client.key('EntityKind')
    entity = datastore.Entity(key)
    entity.update({'request': x})
    client.put(entity)

if __name__ == '__main__':
    #obj = lambda: None
    #obj.data = lambda: "abcxyz"
    #processRequest(obj)
    saveData([123])