Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query datetime by today's date in Django

Tags:

python

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()) 
like image 881
Brenden Avatar asked Aug 27 '11 22:08

Brenden


People also ask

How do I get today's date in Django?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.

How can I filter a date of a DateTimeField in Django?

To filter a date of a DateTimeField in Python Django, we can use filter with a datetime. to call filter to return the results with the datetime_published field set to 2018-03-27.

How do I filter query objects by time range in Django?

To filter query objects by date range in Python Django, we can use the filter method. to call objects. filter with the date__range parameter set to a list with the date range with the strings as the date values. date is the column name we're filtering by.

What is Django Q?

Django Q is a native Django task queue, scheduler and worker application using Python multiprocessing.


1 Answers

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)) 
like image 138
Ismail Badawi Avatar answered Sep 28 '22 09:09

Ismail Badawi