Model filter on date ranges

92 views
Skip to first unread message

sushanth Reddy

unread,
May 23, 2011, 3:45:15 PM5/23/11
to django...@googlegroups.com

Hi,

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

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

------------------------------------------------------

How to do this filter in django.

I have gone through this link ,but its confusing .

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

Thanks in Advance

Ori Livneh

unread,
May 24, 2011, 3:00:49 AM5/24/11
to django...@googlegroups.com
Check out the python-dateutil module (not in the standard lib, but available via pip and easy_install). It makes calculating time deltas easy.

Anyways, try something like this:

#!/usr/bin/env python
from datetime import datetime
from dateutil.relativedelta import relativedelta

employees = {}
now = datetime.now()
ranges = [(0, 3), (3, 6), (6, 12), (12, 24)]

for (a, b) in ranges:
    start_date = now - relativedelta(months=b)
    end_date = now - relativedelta(months=a)
    employees[(a, b)] = Employees.objects.filter(
            doj_date__range=(start_date, end_date))

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

sushanth Reddy

unread,
May 24, 2011, 6:26:07 AM5/24/11
to django...@googlegroups.com
Thank you for the response,Let me give a try and let you know.


Thanks & Regards
sushanth reddy
Reply all
Reply to author
Forward
0 new messages