I'm trying to write a customer date range field for Django admin. This is my filter, which is very similar to the decade example provided in the docs but is not working for me. What am I doing wrong? It just shows all records for every selection.
class DaysSinceAdvertFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('Days Since Advert')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'last_advert'
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
return (
('0', _('Less than 7 days')),
('7', _('7-13 days')),
('14', _('14-20 days')),
('21', _('21-27 days')),
('28', _('28-34 days')),
('35', _('35+ days')),
)
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
# Compare the requested value to decide how to filter the queryset.
today = datetime.date.today()
if self.value() == '0':
return queryset.filter(last_advert__gte=today - datetime.timedelta(days=6))
if self.value() == '7':
return queryset.filter(last_advert__gte=today - datetime.timedelta(days=13),
last_advert__lte=today - datetime.timedelta(days=7))
if self.value() == '14':
return queryset.filter(last_advert__gte=today - datetime.timedelta(days=20),
last_advert__lte=today - datetime.timedelta(days=14))
if self.value() == '21':
return queryset.filter(last_advert__gte=today - datetime.timedelta(days=27),
last_advert__lte=today - datetime.timedelta(days=21))
if self.value() == '28':
return queryset.filter(last_advert__gte=today - datetime.timedelta(days=34),
last_advert__lte=today - datetime.timedelta(days=28))
if self.value() == '35':
return queryset.filter(last_advert__lte=today - datetime.timedelta(days=35))
last_advert is a models.DateField