Bemmu
Bemmu

Reputation: 18247

Filtering by entity key name in Google App Engine on Python

On Google App Engine to query the data store with Python, one can use GQL or Entity.all() and then filter it. So for example these are equivalent

gql = "SELECT * FROM User WHERE age >= 18"
db.GqlQuery(gql)

and

query = User.all()
query.filter("age >=", 18)

Now, it's also possible to query things by key name. I know that in GQL you do it like this

gql = "SELECT * FROM User WHERE __key__ >= Key('User', 'abc')"
db.GqlQuery(gql)

But how would you now use filter to do the same?

query = User.all()
query.filter("__key__ >=", ?????)

Upvotes: 15

Views: 6106

Answers (2)

Hannes
Hannes

Reputation: 33

For me a similar way worked, while the "from_path" function did not worked in my case, this one did:

from google.appengine.api.datastore import Key

key = Key(your_string_with_key)
query.filter("__key__ = ", key)

Upvotes: 3

Bemmu
Bemmu

Reputation: 18247

from google.appengine.api.datastore import Key
query.filter("__key__ >=", Key.from_path('User', 'abc'))

Upvotes: 16

Related Questions