Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored May 03, 2011
5  django/contrib/admin/filters.py
@@ -63,7 +63,10 @@ def __init__(self, request, params, model, model_admin):
63 63
             raise ImproperlyConfigured(
64 64
                 "The list filter '%s' does not specify "
65 65
                 "a 'parameter_name'." % self.__class__.__name__)
66  
-        self.lookup_choices = self.lookups(request)
  66
+        lookup_choices = self.lookups(request)
  67
+        if lookup_choices is None:
  68
+            lookup_choices = ()
  69
+        self.lookup_choices = lookup_choices
67 70
 
68 71
     def has_output(self):
69 72
         return len(self.lookup_choices) > 0
15  docs/ref/contrib/admin/index.txt
@@ -607,15 +607,12 @@ subclass::
607 607
                   class AuthDecadeBornListFilter(DecadeBornListFilter):
608 608
 
609 609
                       def lookups(self, request):
610  
-                          if request.user.is_authenticated():
611  
-                              return (
612  
-                                  ('80s', 'in the eighties'),
613  
-                                  ('other', 'other'),
614  
-                              )
615  
-                          else:
616  
-                              return (
617  
-                                  ('90s', 'in the nineties'),
618  
-                              )
  610
+                          if request.user.is_superuser:
  611
+                              return super(AuthDecadeBornListFilter, self).lookups(request)
  612
+
  613
+                      def queryset(self, request, queryset):
  614
+                          if request.user.is_superuser:
  615
+                              return super(AuthDecadeBornListFilter, self).queryset(request, queryset)
619 616
 
620 617
         * a tuple, where the first element is a field name and the second
621 618
           element is a class inheriting from
19  tests/regressiontests/admin_filters/tests.py
@@ -43,6 +43,11 @@ class DecadeListFilterWithoutTitle(DecadeListFilter):
43 43
 class DecadeListFilterWithoutParameter(DecadeListFilter):
44 44
     title = 'publication decade'
45 45
 
  46
+class DecadeListFilterWithNoneReturningLookups(DecadeListFilterWithTitleAndParameter):
  47
+
  48
+    def lookups(self, request):
  49
+        pass
  50
+
46 51
 class CustomUserAdmin(UserAdmin):
47 52
     list_filter = ('books_authored', 'books_contributed')
48 53
 
@@ -60,6 +65,9 @@ class DecadeFilterBookAdminWithoutTitle(ModelAdmin):
60 65
 class DecadeFilterBookAdminWithoutParameter(ModelAdmin):
61 66
     list_filter = (DecadeListFilterWithoutParameter,)
62 67
 
  68
+class DecadeFilterBookAdminWithNoneReturningLookups(ModelAdmin):
  69
+    list_filter = (DecadeListFilterWithNoneReturningLookups,)
  70
+
63 71
 class ListFiltersTests(TestCase):
64 72
 
65 73
     def setUp(self):
@@ -453,3 +461,14 @@ def test_simplelistfilter_without_parameter(self):
453 461
         self.assertRaisesRegexp(ImproperlyConfigured,
454 462
             "The list filter 'DecadeListFilterWithoutParameter' does not specify a 'parameter_name'.",
455 463
             self.get_changelist, request, Book, modeladmin)
  464
+
  465
+    def test_simplelistfilter_with_none_returning_lookups(self):
  466
+        """
  467
+        A SimpleListFilter lookups method can return None but disables the
  468
+        filter completely.
  469
+        """
  470
+        modeladmin = DecadeFilterBookAdminWithNoneReturningLookups(Book, site)
  471
+        request = self.request_factory.get('/', {})
  472
+        changelist = self.get_changelist(request, Book, modeladmin)
  473
+        filterspec = changelist.get_filters(request)[0]
  474
+        self.assertEqual(len(filterspec), 0)

0 notes on commit 05b4f2e

Please sign in to comment.
Something went wrong with that request. Please try again.