I’ve got a field in one model like:
JavaScript
x
3
1
class Sample(models.Model):
2
date = fields.DateField(auto_now=False)
3
Now, I need to filter the objects by a date range.
How do I filter all the objects that have a date between 1-Jan-2011
and 31-Jan-2011
?
Advertisement
Answer
Use
JavaScript
1
2
1
Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])
2
Or if you are just trying to filter month wise:
JavaScript
1
3
1
Sample.objects.filter(date__year='2011',
2
date__month='01')
3
Edit
As Bernhard Vallant said, if you want a queryset which excludes the specified range ends
you should consider his solution, which utilizes gt/lt (greater-than/less-than).