gpt4 book ai didi

google-app-engine - 如何将 ndb 键与 integer_id 一起使用?

转载 作者:行者123 更新时间:2023-12-02 06:03:47 25 4
gpt4 key购买 nike

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

Returns the integer id in the last (kind, id) pair, or None if the key has an string id or is incomplete.

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

    r = ndb.Key(UserSession, int(id)).get()
if r:
return r.session

但是 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

我改变了 int(id) -> str(id)

似乎是对的;

所以我的问题是,如何将 ndb 键与 integer_id 一起使用?

模型是

class UserSession(ndb.Model):
session = ndb.BlobProperty()

最佳答案

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

关于google-app-engine - 如何将 ndb 键与 integer_id 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15187813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com