wbruntra wbruntra - 7 months ago 34
Python Question

Getting list of connected entities using ndb KeyProperty

Using Google App Engine for Python. It seems that when you had connected datastore entities under the old db model you could rather easily get a list of connected entities, but I can't seem to do the same thing with ndb.

I have:

class User(ndb.Model):
username = ndb.StringProperty()

class Collection(ndb.Model):
owner = ndb.KeyProperty(User)
name = ndb.StringProperty()
photos = ndb.StringProperty(repeated=True)

So each collection is created with an owner. I thought I would be able to get the User's collections just with:

collections = user.collections

But that results in:

collections = user.collections

AttributeError: 'User' object has no attribute 'collections'

From the documentation, it seems that you could do this on db by specifying the collection_name keyword, but I do not see the ndb equivalent.


ndb does not create back-references. But it's just as easy to do the query forwards: