1

Assume:

class Pet (db.model):
  owner = db.ReferenceProperty(User)
  vet = db.ReferenceProperty(Vet)
  name = db.StringProperty()

How do I query for Pets that have user Owner A and Vet B? I assume the references in the Pet class are keys. Please post a reference if you know a good one - I could not find a good example in the docs.

Will Curran
  • 6,959
  • 15
  • 59
  • 92

3 Answers3

2

They are keys.

And you can get them from a model instance or you can generate them from scratch.

Filter on a key: Question GQL ReferenceProperty filter

db.GqlQuery("SELECT * FROM Pet WHERE owner = :1 and vet = :2", owner.key(), vet.key())

Pet.all().filter("owner =", owner.key()).filter("vet =", vet.key())

Create keys using from_path: Datastore key.from_path

Key.from_path(kind, id_or_name, parent=none, namespace=None, **kwds)
Key.from_path('Pet', 'Dr Vet')
Key.from_path('Pet', 123)
Community
  • 1
  • 1
kevpie
  • 25,206
  • 2
  • 24
  • 28
0

After some trial and error I found the solution was straight forward:

q = Pet.all()
q.filter("owner", owner)
q.filter("vet", vet)
q.get()
Will Curran
  • 6,959
  • 15
  • 59
  • 92
0

Have a look to Gqlquery class:

db.GqlQuery("SELECT * FROM Pet WHERE owner = :1 AND vet = :2", user.key(), vet.key())
systempuntoout
  • 71,966
  • 47
  • 171
  • 241