-2

I have two classes in this way:

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)

so i get the comment from the user and then append it to the list comments in report class, this what i do:

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

unfortunately this don't work i get this error:

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

please help me !

       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'')
user3376321
  • 849
  • 2
  • 9
  • 21
  • What error, you have posted the representation of a comment object. – Tim Hoffman Mar 25 '14 at 13:39
  • i am sorry, i edit it -> BadValueError: Expected string, got comment(key=Key('comment', 5659645909663744), content=u'dasdsad', date=u'', name=u'', title=u'') – user3376321 Mar 25 '14 at 13:42
  • I assume the stacktrace point's to the line of code `report.comments.append(new_comment)` ? – Tim Hoffman Mar 25 '14 at 13:59
  • so what should i do, and why it expected string not object – user3376321 Mar 25 '14 at 14:00
  • Can you confirm thats the case ? It seems odd, but so far you have left out important information ie the error so I am not going to assume anything at the moment. – Tim Hoffman Mar 25 '14 at 14:01
  • I add the full error message ! – user3376321 Mar 25 '14 at 14:03
  • Ok, so the line of code in the error doesn't match the code you have posted here. It's important when you ask questions you are accurate and include the correct information. Can you double check things – Tim Hoffman Mar 25 '14 at 14:05
  • you are right , so i again posted the full erro message ,could you help me ? – user3376321 Mar 25 '14 at 14:08
  • See the line in the stacktrace `report.comments=[comment(date="date",title="title",name="name",content="content")]` it tells me that the code you are running in the stacktrace is not the code you have listed as the body your `post` handler. The code in the error should work, but it makes me suspect you have an problem elsewhere in your code. – Tim Hoffman Mar 25 '14 at 14:11
  • no no , actually i then just try to put a string not the same string that i get, just to check – user3376321 Mar 25 '14 at 14:13
  • Huh ? I don't understand what you are saying. All I know is your stacktrace code doesn't match your listed code in the question, so I can't trust the rest of the code you have posted here. – Tim Hoffman Mar 25 '14 at 14:15
  • please see again the edit , i am so sorry this the right error – user3376321 Mar 25 '14 at 14:17
  • Can you show me a couple of things - the exact source of line 64 in `upload_comment.py` and double check that the class definitions for `comment` and `report` in your running code actually match what you have posted in the question above. The stacktrace you are supplying suggests that `comments` property is in fact defined as a `ndb.TextProperty`. Also check what version of the SDK you are using. – Tim Hoffman Mar 25 '14 at 14:30
  • yes of course , this the line 64 -> report.comments.append(new_comment) – user3376321 Mar 25 '14 at 14:34
  • and regarding to report and comment, it is the same except that there are some more property in the report class in my source code, that i don't think it is matter, 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() – user3376321 Mar 25 '14 at 14:36
  • But it does very much , see my answer below – Tim Hoffman Mar 25 '14 at 14:37
  • where is the answer ? – user3376321 Mar 25 '14 at 14:38
  • You have to give me time to put it in. – Tim Hoffman Mar 25 '14 at 14:40

1 Answers1

2

Ok from your last comment we can now see the problem

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

Can you see the problem.

You have defined comments twice and the second definition is the only one that will be used. You have redefined the comments as a StringProperty

Do you see how important it is to be accurate in the information you have provided.

The error message you are getting is telling you the exact problem

Tim Hoffman
  • 12,976
  • 1
  • 17
  • 29
  • I am going to down vote you question, you wasted a lot of time by not including the correct code and the stacktrace in your initial question – Tim Hoffman Mar 25 '14 at 14:41
  • please, could you help me in this question : http://stackoverflow.com/questions/22675683/how-to-append-structured-property-to-list-google-app-engine-datestore – user3376321 Mar 27 '14 at 00:27