mirror of https://github.com/django/django.git
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:
parent
950e05c3ff
commit
05b4f2ebc2
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue