Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django filter by datetime on a range of dates

I have a model with field "created_at", and I have a list of dates. So, I want to get all the models that are created in the date range. How ?

I know that we can compare datetime and date easily using:

queryset.filter(created_at__startswith=date)

But, I have a range of dates, so how ?
Let me know for more information.

like image 569
user2139745 Avatar asked Dec 04 '13 15:12

user2139745


People also ask

What is Django Q?

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

What is Django filter?

Django-filter is a generic, reusable application to alleviate writing some of the more mundane bits of view code. Specifically, it allows users to filter down a queryset based on a model's fields, displaying the form to let them do this. Adding a FilterSet with filterset_class. Using the filterset_fields shortcut.


1 Answers

You can combine date and range field lookups:

queryset.filter(created_at__date__range=(start_date, end_date))
like image 61
Ali Shamakhi Avatar answered Oct 01 '22 23:10

Ali Shamakhi