dabadaba dabadaba - 2 years ago 157
Python Question

Django: datetime filter by date ignoring time

I am trying to filter

es scheduled in a certain day. I can't just do this:


because it also filters by time, but I can do this:

match_queryset.filter(start__year=a_date.year, start__month=a_date.month, start__day=a_date.day)

But this is too complex, I feel like there could be a simpler way.

Then I can also use a range:

t0 = datetime.datetime.combine(a_date, datetime.time(0, 0))
t1 = datetime.datetime.combine(a_date, datetime.time(23, 59, 59))
match_queryset.filter(start__gte=t0, start__lte=t1)

But this just seems to be an overkill and it probably generates an inefficient query.

Can't I just make a query to target the actual date? Something like:

# this of course doesn't work

No need to say I have tried looking for solution and could not find any.

Answer Source

From Django 1.9 you can use the __date field lookup, exactly as you have mentioned in your question. For older versions, you will have to do with the other methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download