mstath
mstath

Reputation: 111

GAE Python TypeError on datastore filter query

I persist such an entity in one RequestHandler and can verify through SDK console:

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

EDIT: I previously only included the partial class definition, and didn't show how I was writing the model. The following is the full example:

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

When I try filtered 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()

I get the following:

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 -

However if I do print q.count() instead of print q.get() I will get 1. Seems I'm doing things correctly according to documentation, and I tried suggestions from similar questions I found but to no avail.

SDK v1.7.3

Upvotes: 0

Views: 680

Answers (1)

mstath
mstath

Reputation: 111

Inspired by a comment left to my question, and some notion that I was doing something wrong, with the addition of some other similar questions/answers found, I re-evaluated the way I was handling the Model class. It turns out at least the way I was overriding the class constructor, I believe I was breaking something in the way the superclass handles initialization. The following works OK for me now:

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

Upvotes: 1

Related Questions