How to customize django DateRangeFilter

475 views
Skip to first unread message

Mitch

unread,
Jun 25, 2011, 11:47:22 AM6/25/11
to django-filter
I would like to change the date options of the django-filters'
DateRangeFilter. I have tried a custom class which overrides the
original init, but I get the error "TypeError at /
member_contact_filtered/: lambda() takes exactly 1 argument (2 given)"
and don't understand where the problem is. This is what I have. Thanks
for any help.

import django_filters
from django_filters.filterset import FilterSet

class CustomDateRangeFilter(django_filters.DateRangeFilter):

def __init__(self, *args, **kwargs):
try:
options = kwargs.pop('options')
self.options = options
except KeyError:
pass
kwargs['choices'] = [(key, value[0]) for key, value in
self.options.iteritems()]
super(CustomDateRangeFilter, self).__init__(*args, **kwargs)

class MemberContactFilter(FilterSet):

from django.utils.translation import ugettext_lazy as _

options = {
'': (_('Any Date'), lambda name: Q()),
1: (_('Today'), lambda name: Q(**{
'%s__year' % name: datetime.today().year,
'%s__month' % name: datetime.today().month,
'%s__day' % name: datetime.today().day
})),
2: (_('Past 7 days'), lambda name: Q(**{
'%s__gte' % name: (datetime.today() -
timedelta(days=7)).strftime('%Y-%m-%d'),
'%s__lt' % name: (datetime.today()+timedelta(days=1)).strftime('%Y-
%m-%d'),
})),
}

next_contact_date = CustomDateRangeFilter(label='Follow up',
options=options)

class Meta:
model = MemberContact
fields = ['next_contact_date']

Mitch

unread,
Jun 25, 2011, 4:05:50 PM6/25/11
to django-filter
Problem solved. I was copying code from the github version of
filters.py which was different from the one I was using.

Daniel Dias

unread,
Mar 6, 2012, 12:34:20 PM3/6/12
to django...@googlegroups.com
I'm Having the same problem
You can help me?
what did you do the fix this?
Reply all
Reply to author
Forward
0 new messages