views:

219

answers:

2

is it possible to ask parent for its refered collection_name based on one of its keys, lets say i have a parent db model and its key, can i know ths children who refer to this parent through collection name or otherwise

class Parent(db.Model):
  user = db.UserProperty()

class Childs(db.Model):
  refer = db.ReferenceProperty(Parent,collection_name='children')
+1  A: 

I think you're asking "can I get the set of all the children that refer to a given parent".

In which case, yes you can, it's a property of the Parent class.

Assuming you have a Parent object p then the children that reference it will be in p.children

If you hadn't specified the collection_name on the ReferenceProperty they would be in p.childs_set

Check out the documentation.

Gareth Simpson
A: 

Yes, you can.

ReferenceProperty has another handy feature: back-references. When a model has a ReferenceProperty to another model, each referenced entity gets a property whose value is a Query that returns all of the entities of the first model that refer to it.

# To fetch and iterate over every Childs entity that refers to the
# Parent instance p:
for child in p.children:
# ...
an0
This answer is correct to a point, except that in the specific example of the question, there is a collection_name set on the Childs' reference property. So in this case instance p has no childs_set, the references are in p.children
Gareth Simpson
Oh, it seems the question was updated after I gave the answer. I've updated the answer to follow that change.
an0