Filtering by entity key name in Google App Engine on Python

人盡茶涼 提交于 2019-11-29 00:35:34

问题


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__ >=", ?????)

回答1:


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



回答2:


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)


来源:https://stackoverflow.com/questions/2544565/filtering-by-entity-key-name-in-google-app-engine-on-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!