Query datetime by today's date in Django

I'm saving datetime in the db for an object. I'd like to query against the db and select anything from todays date, not datetime.

What's the easiest way to do this? This doesn't work:

invoice_for_today = Invoice.objects.get(user=user, date=date.today())

Solution 1:

I remember there being plans to add a __date field lookup to make this easier, but as it stands the "standard" way of doing it is

today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
Invoice.objects.get(user=user, date__range=(today_min, today_max))

Solution 2:

You can also do something like this:

today = date.today()
invoice_for_today = Invoice.objects.filter(date__year=today.year, date__month=today.month, date__day=today.day)

Solution 3:

in django<1.9

from django.utils.timezone import datetime #important if using timezones
today = datetime.today()
foo_for_today = Foo.objects.filter(datefield__year=today.year, datefield__month=today.month, datefield__day=today.day)

in django>1.9, as they added the date keyword

foo_for_today = Foo.objects.filter(datefield__date=datetime.date.today())

Solution 4:

obj = TeachersPlanner.objects.filter(date__startswith=date.today())