Filtering out DateTime in Django can be a huge pain, when, you can’t for the life of you get Django to filter correctly on today’s date.
There is one, simple way of solving this problem.
Once you understand how to do this, you’ll laugh at how easy it was to implement.
Let’s say, for example, that you have a column in a model called post_date.
class MyModel(models.Model): post_date = models.DateTimeField(auto_add_now=True) # ... additional fields ...
How do you filter DateTimeField by today’s date?
The problem is that you have is that you need today’s posts, but it’s tough because your model is implemented as a DateTimeField.
from datetime import date today = date.today() today_filter = MyModel.objects.filter(post_date__year=today.year, post_date__month=today.month, post_date__day=today.day)
Or, you can upgrade your instance of Django to 1.9 where there is a filter specifically for dates.
from datetime import date MyModel.objects.filter(post_date__date=date.today())
The choice is yours! But, now you know exactly what you need to do to filter by date.