数据存储过滤器查询中的GAE Python TypeError

时间:2012-11-04 07:09:11

标签: python google-app-engine google-cloud-datastore

我在一个RequestHandler中保留了这样一个实体,可以通过SDK控制台进行验证:

class Moment(db.Model):
    user = db.IntegerProperty()
    index = db.IntegerProperty()

编辑:我以前只包含了部分类定义,并没有说明我是如何编写模型的。以下是完整示例:

class Moment(db.Model):
    user = db.IntegerProperty()
    index = db.IntegerProperty()
    date = db.DateTimeProperty()
    qx = db.FloatProperty()
    qy = db.FloatProperty()
    qz = db.FloatProperty()
    qw = db.FloatProperty()
    latitude = db.FloatProperty()
    longitude = db.FloatProperty()
    image = db.BlobProperty()
    def __init__(self, obj):
        super(Moment,self).__init__()
        self.user = obj['user']
        self.index = obj['index']
        self.date = obj['date']
        self.qx = obj['qx']
        self.qy = obj['qy']
        self.qz = obj['qz']
        self.qw = obj['qw']
        self.latitude = obj['latitude']
        self.longitude = obj['longitude']
        self.image = obj['image']

class UploadHandler(webapp2.RequestHandler):
    def post(self):
        obj = biplist.readPlistFromString(self.request.body)
        Moment(obj).put()

当我尝试过滤get ...

class ServeHandler(webapp2.RequestHandler):
    def get(self):
        params = {}
        params['user'] = int(self.request.get('user'))
        params['index'] = int(self.request.get('index'))
        q = Moment.all()
        q.filter("user =", params['user'])
        q.filter("index =", params['index'])
        print q.get()

我得到以下内容:

ERROR    2012-11-04 06:56:04,846 webapp2.py:1553] __init__() got an unexpected keyword argument 'index'
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "<path>/main.py", line 51, in get
    print q.get()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 2102, in get
    return results.next()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 2314, in next
    return self.__model_class.from_entity(self.__iterator.next())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 1442, in from_entity
    return cls(None, _from_entity=entity, **entity_values)
TypeError: __init__() got an unexpected keyword argument 'index'
INFO     2012-11-04 06:56:04,849 dev_appserver.py:3092] "GET /serve?user=0&index=0 HTTP/1.1" 500 -

但是,如果我print q.count()代替print q.get(),我会得到1.似乎我根据文档正确地做事情,我尝试了我发现的类似问题的建议,但无济于事。< / p>

SDK v1.7.3

1 个答案:

答案 0 :(得分:1)

受到我的问题留下的评论以及我做错事的一些想法的启发,添加了一些其他类似的问题/答案,我重新评估了我处理Model类的方式。事实证明,至少我覆盖了类构造函数的方式,我相信我正在破坏超类处理初始化的方式。以下工作对我来说还可以:

class Moment(db.Model):
    user = db.IntegerProperty()
    index = db.IntegerProperty()
    date = db.DateTimeProperty()
    qx = db.FloatProperty()
    qy = db.FloatProperty()
    qz = db.FloatProperty()
    qw = db.FloatProperty()
    latitude = db.FloatProperty()
    longitude = db.FloatProperty()
    image = db.BlobProperty()

class UploadHandler(webapp2.RequestHandler):
    def post(self):
        obj = biplist.readPlistFromString(self.request.body)
        Moment(user = obj['user'],
               index = obj['index'],
               date = obj['date'],
               qx = obj['qx'],
               qy = obj['qy'],
               qz = obj['qz'],
               qw = obj['qw'],
               latitude = obj['latitude'],
               longitude = obj['longitude'],
               image = obj['image']).put()