Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16080 -- Handle admin list filter items with two characters be…

…tter. Thanks, Ales Zoulek and Julien Phalip.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4a4b6b271e681ce1c68bc0f10eb90355896e1a91 1 parent c0303f5
Jannis Leidel authored May 24, 2011
12  django/contrib/admin/validation.py
@@ -73,18 +73,16 @@ def validate(cls, model):
73 73
                             " associated with a field name."
74 74
                             % (cls.__name__, idx, item.__name__))
75 75
             else:
76  
-                try:
77  
-                    # Check for option #2 (tuple)
78  
-                    field, list_filter_class = item
79  
-                except (TypeError, ValueError):
80  
-                    # item is option #1
81  
-                    field = item
82  
-                else:
  76
+                if isinstance(item, (tuple, list)):
83 77
                     # item is option #2
  78
+                    field, list_filter_class = item
84 79
                     if not issubclass(list_filter_class, FieldListFilter):
85 80
                         raise ImproperlyConfigured("'%s.list_filter[%d][1]'"
86 81
                             " is '%s' which is not of type FieldListFilter."
87 82
                             % (cls.__name__, idx, list_filter_class.__name__))
  83
+                else:
  84
+                    # item is option #1
  85
+                    field = item
88 86
                 # Validate the field string
89 87
                 try:
90 88
                     get_fields_from_path(model, field)
6  django/contrib/admin/views/main.py
@@ -97,10 +97,10 @@ def get_filters(self, request, use_distinct=False):
97 97
                         self.model, self.model_admin)
98 98
                 else:
99 99
                     field_path = None
100  
-                    try:
101  
-                        # This is custom FieldListFilter class for a given field.
  100
+                    if isinstance(list_filer, (tuple, list)):
  101
+                        # This is a custom FieldListFilter class for a given field.
102 102
                         field, field_list_filter_class = list_filer
103  
-                    except (TypeError, ValueError):
  103
+                    else:
104 104
                         # This is simply a field name, so use the default
105 105
                         # FieldListFilter class that has been registered for
106 106
                         # the type of the given field.
1  tests/regressiontests/admin_filters/models.py
@@ -8,6 +8,7 @@ class Book(models.Model):
8 8
     contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)
9 9
     is_best_seller = models.NullBooleanField(default=0)
10 10
     date_registered = models.DateField(null=True)
  11
+    no = models.IntegerField(verbose_name=u'number', blank=True, null=True) # This field is intentionally 2 characters long. See #16080.
11 12
 
12 13
     def __unicode__(self):
13 14
         return self.title
25  tests/regressiontests/admin_filters/tests.py
@@ -66,7 +66,7 @@ class CustomUserAdmin(UserAdmin):
66 66
     list_filter = ('books_authored', 'books_contributed')
67 67
 
68 68
 class BookAdmin(ModelAdmin):
69  
-    list_filter = ('year', 'author', 'contributors', 'is_best_seller', 'date_registered')
  69
+    list_filter = ('year', 'author', 'contributors', 'is_best_seller', 'date_registered', 'no')
70 70
     order_by = '-id'
71 71
 
72 72
 class DecadeFilterBookAdmin(ModelAdmin):
@@ -100,8 +100,8 @@ def setUp(self):
100 100
 
101 101
         # Books
102 102
         self.djangonaut_book = Book.objects.create(title='Djangonaut: an art of living', year=2009, author=self.alfred, is_best_seller=True, date_registered=self.today)
103  
-        self.bio_book = Book.objects.create(title='Django: a biography', year=1999, author=self.alfred, is_best_seller=False)
104  
-        self.django_book = Book.objects.create(title='The Django Book', year=None, author=self.bob, is_best_seller=None, date_registered=self.today)
  103
+        self.bio_book = Book.objects.create(title='Django: a biography', year=1999, author=self.alfred, is_best_seller=False, no=207)
  104
+        self.django_book = Book.objects.create(title='The Django Book', year=None, author=self.bob, is_best_seller=None, date_registered=self.today, no=103)
105 105
         self.gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002, is_best_seller=True, date_registered=self.one_week_ago)
106 106
         self.gipsy_book.contributors = [self.bob, self.lisa]
107 107
         self.gipsy_book.save()
@@ -528,3 +528,22 @@ def test_simplelistfilter_with_queryset_based_lookups(self):
528 528
         self.assertEqual(choices[2]['display'], u'the 2000\'s')
529 529
         self.assertEqual(choices[2]['selected'], False)
530 530
         self.assertEqual(choices[2]['query_string'], '?publication-decade=the+00s')
  531
+
  532
+    def test_two_characters_long_field(self):
  533
+        """
  534
+        Ensure that list_filter works with two-characters long field names.
  535
+        Refs #16080.
  536
+        """
  537
+        modeladmin = BookAdmin(Book, site)
  538
+        request = self.request_factory.get('/', {'no': '207'})
  539
+        changelist = self.get_changelist(request, Book, modeladmin)
  540
+
  541
+        # Make sure the correct queryset is returned
  542
+        queryset = changelist.get_query_set(request)
  543
+        self.assertEqual(list(queryset), [self.bio_book])
  544
+
  545
+        filterspec = changelist.get_filters(request)[0][-1]
  546
+        self.assertEqual(force_unicode(filterspec.title), u'number')
  547
+        choices = list(filterspec.choices(changelist))
  548
+        self.assertEqual(choices[2]['selected'], True)
  549
+        self.assertEqual(choices[2]['query_string'], '?no=207')
1  tests/regressiontests/modeladmin/models.py
@@ -34,6 +34,7 @@ class ValidationTestModel(models.Model):
34 34
     is_active = models.BooleanField()
35 35
     pub_date = models.DateTimeField()
36 36
     band = models.ForeignKey(Band)
  37
+    no = models.IntegerField(verbose_name="Number", blank=True, null=True) # This field is intentionally 2 characters long. See #16080.
37 38
 
38 39
     def decade_published_in(self):
39 40
         return self.pub_date.strftime('%Y')[:3] + "0's"
2  tests/regressiontests/modeladmin/tests.py
@@ -911,7 +911,7 @@ class ValidationTestModelAdmin(ModelAdmin):
911 911
         # Valid declarations below -----------
912 912
 
913 913
         class ValidationTestModelAdmin(ModelAdmin):
914  
-            list_filter = ('is_active', AwesomeFilter, ('is_active', BooleanFieldListFilter))
  914
+            list_filter = ('is_active', AwesomeFilter, ('is_active', BooleanFieldListFilter), 'no')
915 915
 
916 916
         validate(ValidationTestModelAdmin, ValidationTestModel)
917 917
 

0 notes on commit 4a4b6b2

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