如何在integer_id中使用ndb键?

zur*_*roc 2 google-app-engine app-engine-ndb google-cloud-datastore

我看到了文档 https://developers.google.com/appengine/docs/python/ndb/keyclass#Key_integer_id

返回最后一个(kind,id)对中的整数id,如果密钥具有字符串id或不完整,则返回None.

看,我认为一个键的id可以是一个int; 所以我写

    r = ndb.Key(UserSession, int(id)).get()
    if r:
        return r.session
Run Code Online (Sandbox Code Playgroud)

但是dev_server.py会一直提升

  File "/home/bitcoin/down/google_appengine/google/appengine/datastore/datastore_stub_util.py", line 346, in CheckReference
    raise datastore_errors.BadRequestError('missing key id/name')
BadRequestError: missing key id/name
Run Code Online (Sandbox Code Playgroud)

我支持int(id) - > str(id)

似乎是正确的

所以我的问题是,如何使用整数_id的ndb键?

这个模型是

class UserSession(ndb.Model):
    session = ndb.BlobProperty()
Run Code Online (Sandbox Code Playgroud)

Gui*_*sum 10

读取实体时使用的id类型必须与编写实体时使用的id类型相匹配.通常,在编写新实体时自动分配整数id,而不指定id或key; 然后从entity.put()返回的密钥中获取id.通常不建议分配您自己的整数ID; 当app分配键时,惯例是它们应该是字符串.