Appengine:TypeError:'NoneType'对象不支持项目分配

时间:2013-12-05 07:59:32

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

通过构造函数指定值。但得到“TypeError:'NoneType'对象不支持项目分配”。

import webapp2
from google.appengine.ext import ndb

class test(ndb.Model):
    status = ndb.StringProperty(default=None)

    def __init__(self):
         self.status = "Test Status"


class MainPage(webapp2.RequestHandler):
     def get(self):
          a = test()
          a.put()           


app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

错误:

  File "/home/user/Appengine/guestbook/guestbook.py", line 15, in get
    a = test()
  File "/home/user/Appengine/guestbook/guestbook.py", line 10, in __init__
    self.status = "Test Status"
  File "/home/user/google_appengine/google/appengine/ext/ndb/model.py", line 1265, in __set__
    self._set_value(entity, value)
  File "/home/user/google_appengine/google/appengine/ext/ndb/model.py", line 1011, in _set_value
    self._store_value(entity, value)
  File "/home/user/google_appengine/google/appengine/ext/ndb/model.py", line 992, in _store_value
    entity._values[self._name] = value
TypeError: 'NoneType' object does not support item assignment

2 个答案:

答案 0 :(得分:1)

您可能需要通过调用ndb.Model

来正确初始化__init__
class test(ndb.Model):
    status = ndb.StringProperty(default=None)

    def __init__(self):
        ndb.Model.__init__(self)
        self.status = "Test Status"

答案 1 :(得分:1)

如果你想传递任何参数,你需要做

class Test(ndb.Model): # Classes have uppercase first letter...
    status = ndb.StringProperty(default=None)

    def __init__(self, *a, **k):
        super(Test, self).__init__(self)
        self.status = "Test Status"

然后你应该能够做到

a = Test(foo='bar')

然而,我不确定status的事情。