gpt4 book ai didi

google-app-engine - google app engine python ndb 中的 get_by_id 以 None 响应

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:04 35 4
gpt4 key购买 nike

我在 google app engine python ndb 中使用 get_by_id 时遇到问题。

Python 代码

尝试 1

resource = Content.get_by_id(6093630880088064)

资源是

尝试 2

resource = Content.get_by_id(6093630880088064, parent = 5249205949956096)

BadValueError: Expected Key instance, got 5249205949956096L

尝试 3

key_parent = ndb.Key('Subject', '5249205949956096')
resource = Content.get_by_id(6093630880088064, parent = key_parent)

资源是

数据存储

Entity Kind       Content
Entity Key ahBkZXZ-YnJhaW5ib290ZWNocigLEgdTdWJqZWN0GICAgICAxKkJDAsSB0NvbnRlbnQYgICAgIDE6QoM
ID 6093630880088064
Parent ahBkZXZ-YnJhaW5ib290ZWNochQLEgdTdWJqZWN0GICAgICAxKkJDA
Subject:id=5249205949956096

如有任何建议,我们将不胜感激。我的目标是资源将成为一个对象,我可以在其中执行类似 resource.name 的操作来检索 name 属性。

最佳答案

当您将 key 作为字符串时会出现此问题,最简单的解决方案是:

Model.get_by_id(int(id))

这个问题最有可能出现在你从url之类的地方读取id的时候:

class MyHandler(webapp2.RequestHandler):
def get(self, id):
instance = Model.get_by_id(int(id))

关于google-app-engine - google app engine python ndb 中的 get_by_id 以 None 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925932/

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