Mitch
unread,Jun 25, 2011, 11:47:22 AM6/25/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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']