我想更改django-filters的DateRangeFilter的日期选项。我已经尝试了一个覆盖原始init的自定义类,但是我得到错误“/ member_contact_filtered /:lambda()中的TypeError只需要1个参数(给定2个)”并且不明白问题出在哪里。这就是我所拥有的:
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']
答案 0 :(得分:0)
我使用的filters.py与我在github上看到的不同。哦,好吧。