abhishek February 2016

filter by id in ndb model

I am using ndb model as my databse. what i am trying to do is that filter the result on the basis of list of ids. i have my model like :

class Photo(ndb.Model):
   userid = ndb.StringProperty()

   source = ndb.StringProperty()
   handle = ndb.StringProperty()
   sourceid =ndb.StringProperty()

So i am trying query like this:

              queryset=Photo.query(Photo.key.id().IN(photoid_list))

I have also tried :

              queryset=Photo.query(Photo.id().IN(photoid_list))

where photoid_list is the list of ids. Help me out to solve it.

Answers


Anentropic February 2016

There are examples in the docs:
https://cloud.google.com/appengine/docs/python/ndb/queries#neq_and_in

So it looks like your query should be:

queryset=Photo.query(Photo.id.IN(photoid_list))


Brent Washburne February 2016

I would suggest that you create keys from each id and then get them all at once:

photo_keys = [ndb.Key(Photo, id) for id in photoid_list]
photos = ndb.get_multi(photo_keys)

The advantage is that a get is faster than a query. Also, ndb will memcache the entities by key and make subsequent gets even faster.

Post Status

Asked in February 2016
Viewed 2,003 times
Voted 13
Answered 2 times

Search




Leave an answer