Reputation: 2136
I want to store data in a parent child hierarchy. I want to do so using the ndb.put_multi function. The problem is how will i get the corresponding keys of the parents after i have stored parent models using put multi. Kindly suggest a solution
Upvotes: 1
Views: 362
Reputation: 13138
The trick is to store a "grandparent" or root entity, and have all parent entities reference it in their key:
root = BaseClass()
root.put()
parent = Parent(parent=root)
parent.put()
child = Child(parent=parent)
child.put()
Now you can examine the child's key using the Instance methods:
parent = child.key.parent()
ancestors = child.key.pairs()
The reason you want a root entity is for Ancestor queries:
parents = Parent.query(ancestor=root)
children = Child.query(ancestor=root)
You can then examine the child.key.parent()
to see its parent key.
Upvotes: 0
Reputation: 16825
If you have already used put_multi for the parent keys then you already have them.
Eg
list_of_parent_keys = ndb.put_multi(list_of_entities)
child_keys = []
for parent_key in list_of_parent_keys:
child_key = ndb.Key(Parent, parent_key, Child, child_key)
child_keys.append(child_key)
Upvotes: 4