Django: Model Filter by Date Range

How to get a model filter on date ranges. In my project using a doj employee, I need to deign a table. as an employee who joined less than three months, 3-6 months, 6-12 months, 12-24 months.

Depart < 3month 3-6months 6-12months 12-24months ----------------------------------------- ---- ----- A dep 4 6 6 8 ------------------------------------------------------ 

How to make this filter in django.

I went through this link, but it is confusing.

http://www.nerdydork.com/django-filter-model-on-date-range.html

Thanks at Advance

+6
source share
1 answer

The range filter works as follows:

 MyModel.objects.filter(date__range=(range_start, range_end)) 

You can use this in conjunction with datetime.timedelta to get monthly periods. For instance:

 from datetime import datetime, timedelta now = datetime.now() # <3 months three_months_ago = now - timedelta(months=3) MyModel.objects.filter(date__range=(three_months_ago, now)) 
+19
source

Source: https://habr.com/ru/post/888850/


All Articles