开发者

App Engine, Python: how to filter query by ID?

I try to get data from app engine datastore.

Filtering query by 'title' (or any other property) works:

开发者_Python百科obj = db.Query(PageModel).filter('title',title)[0]

But the same thing with ID - doesn't:

obj = db.Query(PageModel).filter('ID',page_id)[0]

I think there is something special about IDs and KEYs in datastore, but I cant find, how to implement getting data by ID.


Try

obj = PageModel.get_by_id(page_id)

instead. This assumes that the ID you're working with is the numerical ID of a datastore key (ie, came from something like obj.key().id()) and not some arbitrary ID field you've added to your PageModel.


You can filter data by using key(s):

from google.appengine.ext.db import Key
key = Key('bgakaWdyc3NlcnIPCxIJRmVlZEVudHJ5GAwM')

# Match many
PageModel.all().filter('__key__ IN ', [key])
# Match one
PageModel.all().filter('__key__ = ', key) 

However, the method get_by_id(id) is preferred when fetching one entry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜