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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With