保存 ndb LocalStructured 实体时出现 App Engine BadValueError
App Engine BadValueError when saving ndb LocalStructured entity
我有模型
class Foo(ndb.Model):
x = ndb.IntegerProperty()
class Bar(ndb.Model):
foo = ndb.StructuredProperty(Foo, repeated=True)
我最近一直在尝试保存 Bar 实体时,仅在生产中出现此错误:
BadValueError: Expected Foo instance, got Foo(x=100)
我记得前段时间看到过这个错误,然后就消失了。这是什么原因?
您所描述内容的最小版本:
from google.appengine.ext import ndb
import webapp2
class Foo(ndb.Model):
x = ndb.IntegerProperty()
class Bar(ndb.Model):
foo = ndb.StructuredProperty(Foo, repeated=True)
class Doit(webapp2.RequestHandler):
def get(self):
bar = Bar(foo=[Foo(x=100)])
k = bar.put()
self.response.write('Wrote %s' % k)
app = webapp2.WSGIApplication([
('/', Doit),
], debug=True)
如预期的那样运行良好。请添加重现您的问题所需的任何进一步的最小代码量,否则您将无法为我们提供帮助。 (理想情况下,编辑您的问题以包括一个最小的、完整的应用程序来重现您的问题,并让我知道您已经完成了对这个答案的评论——谢谢!)。
顺便说一下,我注意到在您提到的主题中 LocalStructured
,但在您问题的代码中,您实际上使用的是 StructuredProperty
——我在这里展示的代码有效无论如何,如果使用 LocalStructuredProperty
而不是 StructuredProperty
也一样,但是,如果你能澄清歧义,谢谢。
问题是我在保存模型的文件中使用了 models.py 的相对导入,所以不知何故 python 认为 Foo 与 Foo 不同,因为它们在不同的包中。我将模型导入更改为绝对导入,现在它工作正常。
我有模型
class Foo(ndb.Model):
x = ndb.IntegerProperty()
class Bar(ndb.Model):
foo = ndb.StructuredProperty(Foo, repeated=True)
我最近一直在尝试保存 Bar 实体时,仅在生产中出现此错误:
BadValueError: Expected Foo instance, got Foo(x=100)
我记得前段时间看到过这个错误,然后就消失了。这是什么原因?
您所描述内容的最小版本:
from google.appengine.ext import ndb
import webapp2
class Foo(ndb.Model):
x = ndb.IntegerProperty()
class Bar(ndb.Model):
foo = ndb.StructuredProperty(Foo, repeated=True)
class Doit(webapp2.RequestHandler):
def get(self):
bar = Bar(foo=[Foo(x=100)])
k = bar.put()
self.response.write('Wrote %s' % k)
app = webapp2.WSGIApplication([
('/', Doit),
], debug=True)
如预期的那样运行良好。请添加重现您的问题所需的任何进一步的最小代码量,否则您将无法为我们提供帮助。 (理想情况下,编辑您的问题以包括一个最小的、完整的应用程序来重现您的问题,并让我知道您已经完成了对这个答案的评论——谢谢!)。
顺便说一下,我注意到在您提到的主题中 LocalStructured
,但在您问题的代码中,您实际上使用的是 StructuredProperty
——我在这里展示的代码有效无论如何,如果使用 LocalStructuredProperty
而不是 StructuredProperty
也一样,但是,如果你能澄清歧义,谢谢。
问题是我在保存模型的文件中使用了 models.py 的相对导入,所以不知何故 python 认为 Foo 与 Foo 不同,因为它们在不同的包中。我将模型导入更改为绝对导入,现在它工作正常。