Corrected the behavior of the SimpleFilter.lookups method to also be able to return None. Also modified example in documentation to be a bite more realistic. Refs #5833. Thanks for the hint, Martin Mahner.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16150 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-05-03 13:52:39 +00:00
parent 950e05c3ff
commit 05b4f2ebc2
3 changed files with 29 additions and 10 deletions

View File

@ -63,7 +63,10 @@ class SimpleListFilter(ListFilter):
raise ImproperlyConfigured( raise ImproperlyConfigured(
"The list filter '%s' does not specify " "The list filter '%s' does not specify "
"a 'parameter_name'." % self.__class__.__name__) "a 'parameter_name'." % self.__class__.__name__)
self.lookup_choices = self.lookups(request) lookup_choices = self.lookups(request)
if lookup_choices is None:
lookup_choices = ()
self.lookup_choices = lookup_choices
def has_output(self): def has_output(self):
return len(self.lookup_choices) > 0 return len(self.lookup_choices) > 0

View File

@ -607,15 +607,12 @@ subclass::
class AuthDecadeBornListFilter(DecadeBornListFilter): class AuthDecadeBornListFilter(DecadeBornListFilter):
def lookups(self, request): def lookups(self, request):
if request.user.is_authenticated(): if request.user.is_superuser:
return ( return super(AuthDecadeBornListFilter, self).lookups(request)
('80s', 'in the eighties'),
('other', 'other'), def queryset(self, request, queryset):
) if request.user.is_superuser:
else: return super(AuthDecadeBornListFilter, self).queryset(request, queryset)
return (
('90s', 'in the nineties'),
)
* a tuple, where the first element is a field name and the second * a tuple, where the first element is a field name and the second
element is a class inheriting from element is a class inheriting from

View File

@ -43,6 +43,11 @@ class DecadeListFilterWithoutTitle(DecadeListFilter):
class DecadeListFilterWithoutParameter(DecadeListFilter): class DecadeListFilterWithoutParameter(DecadeListFilter):
title = 'publication decade' title = 'publication decade'
class DecadeListFilterWithNoneReturningLookups(DecadeListFilterWithTitleAndParameter):
def lookups(self, request):
pass
class CustomUserAdmin(UserAdmin): class CustomUserAdmin(UserAdmin):
list_filter = ('books_authored', 'books_contributed') list_filter = ('books_authored', 'books_contributed')
@ -60,6 +65,9 @@ class DecadeFilterBookAdminWithoutTitle(ModelAdmin):
class DecadeFilterBookAdminWithoutParameter(ModelAdmin): class DecadeFilterBookAdminWithoutParameter(ModelAdmin):
list_filter = (DecadeListFilterWithoutParameter,) list_filter = (DecadeListFilterWithoutParameter,)
class DecadeFilterBookAdminWithNoneReturningLookups(ModelAdmin):
list_filter = (DecadeListFilterWithNoneReturningLookups,)
class ListFiltersTests(TestCase): class ListFiltersTests(TestCase):
def setUp(self): def setUp(self):
@ -453,3 +461,14 @@ class ListFiltersTests(TestCase):
self.assertRaisesRegexp(ImproperlyConfigured, self.assertRaisesRegexp(ImproperlyConfigured,
"The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'.", "The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'.",
self.get_changelist, request, Book, modeladmin) self.get_changelist, request, Book, modeladmin)
def test_simplelistfilter_with_none_returning_lookups(self):
"""
A SimpleListFilter lookups method can return None but disables the
filter completely.
"""
modeladmin = DecadeFilterBookAdminWithNoneReturningLookups(Book, site)
request = self.request_factory.get('/', {})
changelist = self.get_changelist(request, Book, modeladmin)
filterspec = changelist.get_filters(request)[0]
self.assertEqual(len(filterspec), 0)