0

I'm attempting to model a recursive structure with App Engine NDB:

class Root(ndb.Model):
    pass

class Node(ndb.Model):
    #Node can have either a Root, or another Node as parent
    pass

root_key = Key(Root, 1)
node_a = Key(Root, 1, Node, 2)
node_b = Key(Root, 1, Node, 3)
node_a_a = Key(Root, 1, Node, 2, Node, 4)

From here, I want to query the Root entity for immediate children. What I can do is to query for all descendants of the root:

Node.query(ancestor=root_key) # returns node_a, node_b, and node_a_a

What I'd like to do is:

Node.query(parent=root_key) # returns node_a, node_b

but it seems that querying by (immediate) parent key isn't supported by the ndb api. Hopefully I'm wrong. Looking forward to elucidation. Thanks

patrickcd
  • 110
  • 8

1 Answers1

4

It's not an NDB limitation -- the App Engine datastore can't do this. However you can easily simulate this behavior by including the key (or some other unique ID) of the parent as a property.

Guido van Rossum
  • 16,690
  • 3
  • 46
  • 49