diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index 0e3e2e504c..36dcea1ea7 100644 --- a/django/contrib/admin/filters.py +++ b/django/contrib/admin/filters.py @@ -63,7 +63,10 @@ class SimpleListFilter(ListFilter): raise ImproperlyConfigured( "The list filter '%s' does not specify " "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): return len(self.lookup_choices) > 0 diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 0d28cc48ff..e8bc59724d 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -607,15 +607,12 @@ subclass:: class AuthDecadeBornListFilter(DecadeBornListFilter): def lookups(self, request): - if request.user.is_authenticated(): - return ( - ('80s', 'in the eighties'), - ('other', 'other'), - ) - else: - return ( - ('90s', 'in the nineties'), - ) + if request.user.is_superuser: + return super(AuthDecadeBornListFilter, self).lookups(request) + + def queryset(self, request, queryset): + if request.user.is_superuser: + return super(AuthDecadeBornListFilter, self).queryset(request, queryset) * a tuple, where the first element is a field name and the second element is a class inheriting from diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index b1bd6ded5f..6b32475a64 100644 --- a/tests/regressiontests/admin_filters/tests.py +++ b/tests/regressiontests/admin_filters/tests.py @@ -43,6 +43,11 @@ class DecadeListFilterWithoutTitle(DecadeListFilter): class DecadeListFilterWithoutParameter(DecadeListFilter): title = 'publication decade' +class DecadeListFilterWithNoneReturningLookups(DecadeListFilterWithTitleAndParameter): + + def lookups(self, request): + pass + class CustomUserAdmin(UserAdmin): list_filter = ('books_authored', 'books_contributed') @@ -60,6 +65,9 @@ class DecadeFilterBookAdminWithoutTitle(ModelAdmin): class DecadeFilterBookAdminWithoutParameter(ModelAdmin): list_filter = (DecadeListFilterWithoutParameter,) +class DecadeFilterBookAdminWithNoneReturningLookups(ModelAdmin): + list_filter = (DecadeListFilterWithNoneReturningLookups,) + class ListFiltersTests(TestCase): def setUp(self): @@ -453,3 +461,14 @@ class ListFiltersTests(TestCase): self.assertRaisesRegexp(ImproperlyConfigured, "The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'.", 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)