A sample model:
class Foo(db.Model):
id = db.IntegerProperty()
bar = db.ListProperty(int, required=True)
How can I query using either Query or GqlQuery to return all Foo entities that have a given value in their bar property?
If I have a list of ids, is there a single filter that will return all entities whose id property is in that list?
1.
If you use an equals query on a list property, it will check all items in the list:
search = 2
results = Foo.all().filter('bar =', search).fetch()
2.
You can use an IN filter, but note that internally this makes a datastore query for each item in the list, so it may be slow, and there are also a maximum of 30 internal queries per request.
items = [1, 2, 3]
results = Foo.all().filter("id IN", items).fetch()
See Introducing Queries for details for both 1 and 2, and ListProperty for further details on 1.
For those of you like me who could not get the above answer to work.
Using gae version 1.5.3
results = Foo.all().filter('bar =', search).fetch()
gives no results. But
results = Foo.all().filter('bar =', search).fetch(100)
results = Foo.all().filter('bar =', search)
gives results.
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