Author: jezdez Date: 2011-05-03 06:52:39 -0700 (Tue, 03 May 2011) New Revision: 16150
Modified: django/trunk/django/contrib/admin/filters.py django/trunk/docs/ref/contrib/admin/index.txt django/trunk/tests/regressiontests/admin_filters/tests.py Log: 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. Modified: django/trunk/django/contrib/admin/filters.py =================================================================== --- django/trunk/django/contrib/admin/filters.py 2011-05-03 11:52:42 UTC (rev 16149) +++ django/trunk/django/contrib/admin/filters.py 2011-05-03 13:52:39 UTC (rev 16150) @@ -63,7 +63,10 @@ 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 Modified: django/trunk/docs/ref/contrib/admin/index.txt =================================================================== --- django/trunk/docs/ref/contrib/admin/index.txt 2011-05-03 11:52:42 UTC (rev 16149) +++ django/trunk/docs/ref/contrib/admin/index.txt 2011-05-03 13:52:39 UTC (rev 16150) @@ -607,16 +607,13 @@ 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 :mod:`django.contrib.admin.FieldListFilter`, for example:: Modified: django/trunk/tests/regressiontests/admin_filters/tests.py =================================================================== --- django/trunk/tests/regressiontests/admin_filters/tests.py 2011-05-03 11:52:42 UTC (rev 16149) +++ django/trunk/tests/regressiontests/admin_filters/tests.py 2011-05-03 13:52:39 UTC (rev 16150) @@ -43,6 +43,11 @@ 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 DecadeFilterBookAdminWithoutParameter(ModelAdmin): list_filter = (DecadeListFilterWithoutParameter,) +class DecadeFilterBookAdminWithNoneReturningLookups(ModelAdmin): + list_filter = (DecadeListFilterWithNoneReturningLookups,) + class ListFiltersTests(TestCase): def setUp(self): @@ -453,3 +461,14 @@ 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) -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.