views:

47

answers:

2

Hi all,

Is reverse referencing possible in Google app engine? I am using app engine patch to develope an application and my model is something like:

class Portfolio(db.Model):
   user = db.ReferenceProperty(User)
   pic = db.BlobProperty()

Now, If I have the user object, is it possible to retrieve the pic associated with the users portfolio? i.e. the reverse reference from the User to Portfolio.

+1  A: 

Yes. You can access the pics, via:

user = User()
pics = user.portfolio_set

You can change the default name (which is modelname_set) by passing the collection_name argument to the ReferenceProperty constructor. For example:

class Portfolio(db.Model):
  user = db.ReferenceProperty(User, collection_name="Portfolio")

See more information and examples here: http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html

arikfr
Thanks a lot. It worked.
anand
Glad to help :) Get familiar with the SDK docs -- there are a lot of interesting and helpful comments there.
arikfr
+1  A: 

Yes it is possible. By default you can access users portfolio through user.portfolio_set. Read here for more info: http://code.google.com/intl/pl/appengine/articles/modeling.html

Piotr Duda