【发布时间】:2014-03-27 00:08:01
【问题描述】:
我必须上课评论和报告:
class comment(ndb.Model):
date =ndb.StringProperty()
title=ndb.StringProperty()
name=ndb.StringProperty()
content=ndb.TextProperty()
class report(ndb.Model):
comments=ndb.StructuredProperty(comment,repeated=True)
date=ndb.StringProperty()
title=ndb.StringProperty()
content=ndb.BlobKeyProperty()
images=ndb.BlobKeyProperty(repeated=True)
images_urls=ndb.StringProperty(repeated=True)
所以我将 cmets(在报告类中)声明为 ndb.StructuredProperty ,然后当我从用户那里获得评论时,我以这种方式将其附加到 cmets:
class add(webapp2.RequestHandler):
def post(self):
key_url=self.request.get("key")
key=ndb.Key(urlsafe=key_url)
report=key.get()
title=self.request.get("title")
name=self.request.get("name")
date=self.request.get("date")
content=self.request.get("content")
new_comment=comment(date=date,title=title,name=name,content=content)
report.comments.append(new_comment)
report.put()
self.redirect('/comments?'+urllib.urlencode({"key":key_url}))
实际上,当我部署项目时,它工作正常,但过了一段时间可能 30 分钟它失败了,这很奇怪!我收到此错误消息:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~newseltira/1.374704102870871150/upload_comment.py", line 64, in post
report.put()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3339, in _put
return self._put_async(**ctx_options).get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3351, in _put_async
self._prepare_for_put()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3233, in _prepare_for_put
prop._prepare_for_put(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 2411, in _prepare_for_put
values = self._get_base_value_unwrapped_as_list(entity)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1135, in _get_base_value_unwrapped_as_list
wrapped = self._get_base_value(entity)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1123, in _get_base_value
return self._apply_to_values(entity, self._opt_call_to_base_type)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1295, in _apply_to_values
value[:] = map(function, value)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1177, in _opt_call_to_base_type
value = _BaseValue(self._call_to_base_type(value))
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1198, in _call_to_base_type
return call(value)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1274, in call
newvalue = method(self, value)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 2273, in _validate
(self._modelclass.__name__, value))
BadValueError: Expected comment instance, got comment(content=u'fdsfd ds dsfdsf d', date=u'11/03/2014 03:07:25', name=u'dsfdsf', title=u'dsfdsf')
【问题讨论】:
-
评论实例好像是空的。我不知道为什么这会导致问题,但你想存储空的 cmets。堆栈跟踪确实显示正在调用结构化属性的正确 _validate。我会开始做一些基本的调试,比如在调用 put() 之前检查评论对象以查看发生了什么。
-
对 StructuredProperty 的验证调用在此检查中失败
if not isinstance(value, self._modelclass):,因此您需要找出传递给的值失败的原因。 -
实际上,当有文本时它也会失败,但奇怪的是,当我刚刚部署项目时它工作正常,但过了一会儿它崩溃了!!
-
我在传递值时编辑错误消息
-
为了让它在某些时候工作并且失败,我认为你的代码中还有其他东西会影响正在发生的事情。您是否有使用相同代码的不同处理程序 - 例如,您是否在代码中的其他地方定义了一个类。目前我只是猜测。
标签: google-app-engine python-2.7 google-cloud-datastore