wbruntra
wbruntra

Reputation: 1071

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.

Upvotes: 0

Views: 249

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

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

 Collection.query(Collection.user==user.key)

Upvotes: 1

Related Questions