NDB query.filter

667 views
Skip to first unread message

Moises Belchin

unread,
Jan 10, 2012, 3:49:12 AM1/10/12
to Google App Engine
Hi all,

I'm using NDB query filter and I want to know If it possible store
filters on list and then pass them to NDB.query.filter.
See code below:

################################## BEGIN CODE
#######################################

from google.appengine.ext.ndb import query
from model import MyModel

year_from = 2012
month_from = 1
day_from = 1

year_to = 2012
month_to = 1
day_to = 9

filterDate = []

for i in range(day_from, day_to):
filterDate.append(query.AND(MyModel.day == i,
MyModel.month ==
month_from,
MyModel.year ==
year_from))

q = MyModel.query()
q = q.filter(query.OR(filterDate)) # This raise: TypeError: Cannot
filter a non-Node argument;
result = q.fetch(20)

################################## END CODE
#######################################

My questions:
1. How I convert filterDate to Node argument if possible?
2. Is this code correct and possible with NDB?
3. If not, What is the best practise to implement something similar to
code above?

Thanks so much !

Guido van Rossum

unread,
Jan 10, 2012, 2:37:46 PM1/10/12
to Google App Engine
You're almost there. All you need to do is to change the OR call as
follows:

q = q.filter(query.OR(*filterDate))

This is because OR() doesn't take a list of nodes, it takes one or
more arguments each of which must be a node. The filterDate variable
is a list of nodes. By using *filterDate you invoke Python's variable
argument list feature which generates a separate argument for each
item in the list.

Good luck with your query!

--Guido van Rossum
Reply all
Reply to author
Forward
0 new messages