Reputation: 12445
I have this model:
class Vehicle(db.Model):
...
start_production_date = db.DateProperty()
end_production_date = db.DateProperty()
I need to filter, for example, all vehicles in production within, say, 2010:
I thought I could do:
q = (Vehicle.all()
.filter('start_production_date >=', datetime(2010, 1, 1))
.filter('end_production_date <', datetime(2011, 1, 1)))
buy I get BadFilterError
:
BadFilterError: invalid filter: Only one property per query may have inequality filters (<=, >=, <, >)..
so, how do I acheive this? Moreover this seems to me a quite common task.
Upvotes: 2
Views: 977
Reputation: 12445
I went with this solution:
I set in model a ListProperty
item containing all years the model was crafted:
vhl.production_years = range(start_production_date.year, end_production_date + 1)
Then test:
q = (Vehicle.all()
.filter('production_years =', 2010))
Upvotes: 2
Reputation: 21835
One approach is to change your model to something like this:
class Vehicle(db.Model):
...
start_production_date = db.DateProperty()
start_production_year = db.IntegerProperty()
start_production_month = db.IntegerProperty()
start_production_day = db.IntegerProperty()
end_production_date = db.DateProperty()
end_production_year = db.IntegerProperty()
end_production_month = db.IntegerProperty()
end_production_day = db.IntegerProperty()
Update these new values on every put (you could override put) and the simply:
# specific year
q = (Vehicle.all()
.filter('start_production_year =', 2010))
# specific year, different months
q = (Vehicle.all()
.filter('start_production_year =', 2010)
.filter('start_production_month IN', [1, 2, 3, 4]))
# different years
q = (Vehicle.all()
.filter('start_production_year IN', [2010, 2011, 2012]))
Upvotes: 2