Are you getting validation errors when you try to filter a DateTimeField by today’s date?
It 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.
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, you’re model is implemented as a DateTimeField. You need to match the actual date. In order to do that you need to match the year, month and day explicitly.
But that’s too much typing!
You might think this is way to much to type every time you want want to filter records by date. Are there some things that you can do to make this easier?
Sure! You can create a helper function.
Or, you can upgrade your instance of Django to 1.9 where there is a filter specifically for dates.
The choice is yours! But, now you know exactly what you need to do to filter by date.