Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: datetime filter by date ignoring time

Tags:

python

django

I am trying to filter Matches scheduled in a certain day. I can't just do this:

match_queryset.filter(start=date)

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
match_queryset.filter(start__date=date)

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

like image 336
dabadaba Avatar asked Aug 17 '17 12:08

dabadaba


1 Answers

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.

e.g.

Entry.objects.filter(start__date=datetime.date(2005, 1, 1))
Entry.objects.filter(start__date__gt=datetime.date(2005, 1, 1))
like image 195
shad0w_wa1k3r Avatar answered Sep 20 '22 05:09

shad0w_wa1k3r