Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appengine reverse reference problem

According to the docs: http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References the automatically created reverse reference object is a Query object, so there is possible iteration over it and making fetch calls.

But: I have one model:

class User(db.Model):
   name = db.StringProperty()
   ...

and second model:

class Thing(db.Model):
    owner = db.ReferenceProperty(User)
    ...

And when I try to access reverse reference:

for thing in user.thing_set:
    ...

or:

user.thing_set.fetch(100)

I get an exception like this:

<type 'exceptions.TypeError'>: '_ReverseReferenceProperty' object is not iterable

or like this:

<type 'exceptions.AttributeError'>: '_ReverseReferenceProperty' object has no attribute 'fetch'

Am I doing something wrong or there was some change in appengine? I'm pretty sure that previously it worked like a Query. There is even an example on the docs page, that shows the same usage as mine:

for obj in obj1.secondmodel_set:
    # ...

Additionaly getting the query without reverse reference works ok:

things = Thing.all().filter('owner =', user)
like image 283
Pawel Markowski Avatar asked Nov 14 '22 03:11

Pawel Markowski


1 Answers

Both methods (iterating and fetch) should work. To debug, you might want to log (or print):

print dir(user)
[..., 'thing_set', ...]

print dir(user.thing_set)
[..., '__iter__', ... , 'fetch', ...]

just to see what the objects contain... and that might give you a hint of what could be going wrong.

A couple of ideas:

  • You might be referencing another user class by mistake: http://code.google.com/appengine/docs/python/users/userclass.html
  • Your user object is not saved.
  • In windows, you have some old .pyc files in: C:\Users\YOUR USER NAME\AppData\Local\VirtualStore\Program Files (x86)\Google\google_appengine
like image 170
Amir Avatar answered Dec 15 '22 13:12

Amir