将结构化属性添加到ndb.model:google app engine

时间:2014-03-25 13:18:29

标签: google-app-engine python-2.7 app-engine-ndb

我这样有两个课程:

class comment(ndb.Model):
    date =ndb.StringProperty()
    title=ndb.StringProperty()
    name=ndb.StringProperty()
    content=ndb.StringProperty()

class report(ndb.Model):
    comments=ndb.StructuredProperty(comment,repeated=True)

所以我收到了用户的评论,然后将其附加到报告类的列表评论中,这就是我所做的:

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

不幸的是,这不起作用我得到了这个错误:

 BadValueError: Expected string, got comment(key=Key('comment', 5659645909663744), content=u'dasdsad', date=u'', name=u'', title=u'')

请帮助我!

       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.374670644492631009/upload_comment.py", line 65, 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/tasklets.py", line 325, in get_result
    self.check_success()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 748, in put
    key = yield self._put_batcher.add(entity, options)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 371, in _help_tasklet_along
    value = gen.send(val)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 280, in _put_tasklet
    keys = yield self._conn.async_put(options, datastore_entities)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1791, in async_put
    pbs = [entity_to_pb(entity) for entity in entities]
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 648, in entity_to_pb
    pb = ent._to_pb()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3052, in _to_pb
    prop._serialize(self, pb, projection=self._projection)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1365, in _serialize
    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 1695, in _validate
    (value,))
BadValueError: Expected string, got comment(content=u'dasdsad', date=u'', name=u'', title=u'')

1 个答案:

答案 0 :(得分:2)

从上次评论中我们现在可以看到问题了

 class report(ndb.Model): 
    comments=ndb.StructuredProperty(comment,repeated=True) 
    date=ndb.StringProperty() 
    title=ndb.StringProperty() 
    content=ndb.BlobKeyProperty() 
    comments=ndb.StringProperty(repeated=True) 
    images=ndb.BlobKeyProperty(repeated=True) 
    images_urls=ndb.StringProperty(repeated=True) 
    status=ndb.BooleanProperty()

你能看到问题。

您已定义comments两次,第二个定义是唯一将使用的定义。您已将注释重新定义为StringProperty

您是否认为准确提供所提供的信息非常重要。

您收到的错误消息告诉您确切的问题