How to append structured property to list, google app engine : datestore

馋奶兔 提交于 2019-12-11 06:12:31

问题


I have to classes comment and report:

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)

so i declare comments (in the report class) as ndb.StructuredProperty ,then when i get the comment from the user i append it to the comments in this way:

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}))

actually when I deploy the project it is work fine, but after a while maybe 30 minute it fail , it is weird ! I get this error message:

 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')

回答1:


You said when I asked if you had the class defined elsewhere - you said "yes actually i have , but i am sure that all of them are the same , i do copy paste"

There's your answer.

You should never define the class in more than one place, because if they are imported in different orders you will have problems when doing comparisons.

this is what the stack trace is telling you with

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')

the classes for comment don't match

When building projects your should always adher to the principal of DRY (don't repeat yourself).

Put all your classes in one file and then import them where ever you need to use them.

In this case the validation routine is comparing the classes and whilst they may defined identically they are different entities.



来源:https://stackoverflow.com/questions/22675683/how-to-append-structured-property-to-list-google-app-engine-datestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!