Querying on attributes of related kind in ndb

28 views
Skip to first unread message

Akshar Raaj

unread,
May 11, 2017, 9:01:14 AM5/11/17
to Google App Engine
I have two kinds:

    class Professor(ndb.Model):
        name = ndb.StringProperty()
        email = ndb.StringProperty()

    class Student(ndb.Model):
        professor = ndb.KeyProperty(kind=Professor)
        name = ndb.StringProperty()
        age = ndb.IntegerProperty()

I want to find all the Student entities where Professor name is "Snape". How can I do this?

Do I have to keep professor_name on Student too to achieve what I want?

Or if I take the "One To Many" example of https://cloud.google.com/appengine/articles/modeling. Then I want to find all the PhoneNumber entities where Contact.name is "scott". How to do this.

The appengine example only tells how to filter on PhoneNumber attributes. What if I want to filter on the related Kind, i.e Contact attributes.

Jordan (Cloud Platform Support)

unread,
May 11, 2017, 11:17:58 AM5/11/17
to Google App Engine
You first perform a keys_only query to retrieve the list of all Professors whose 'name' = "Snape". Then query for all Students whose 'professor' is in your key list using the 'professor.IN([key1, key2])' operation.
Reply all
Reply to author
Forward
0 new messages