2011-06-25 79 views
0

我想更改django過濾器的DateRangeFilter的日期選項。我試過一個自定義的類,它覆蓋了原始的init,但是我得到了錯誤「TypeError at/member_contact_filtered /:lambda()只需要1個參數(2給出)」,並且不明白問題出在哪裏。這是我的:如何自定義django DateRangeFilter

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

我使用的filters.py與我在github上看到的不同。好吧。