Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 811
Vladimir Keleshev Avatar asked Feb 17 '10 18:02

Vladimir Keleshev


2 Answers

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.

like image 160
Will McCutchen Avatar answered Oct 16 '22 18:10

Will McCutchen


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.

like image 34
jmu Avatar answered Oct 16 '22 17:10

jmu