Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 05b4f2ebc2f547d2413f738551f9933aae0d34dc 1 parent 950e05c
@jezdez jezdez authored
View
5 django/contrib/admin/filters.py
@@ -63,7 +63,10 @@ def __init__(self, request, params, model, model_admin):
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
View
15 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
View
19 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 @@ def test_simplelistfilter_without_parameter(self):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.