Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15221 -- Made the admin filters on foreign key and m2m relatio…

…nships display the related field's verbose name instead of that of the related model.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16991 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 93a581431a493febf173a8a3f2bd27ab450f228b 1 parent c582609
Julien Phalip authored October 16, 2011
9  django/contrib/admin/filters.py
@@ -146,14 +146,11 @@ class RelatedFieldListFilter(FieldListFilter):
146 146
     def __init__(self, field, request, params, model, model_admin, field_path):
147 147
         super(RelatedFieldListFilter, self).__init__(
148 148
             field, request, params, model, model_admin, field_path)
149  
-
150 149
         other_model = get_model_from_relation(field)
151  
-        if isinstance(field, (models.ManyToManyField,
152  
-                          models.related.RelatedObject)):
153  
-            # no direct field on this model, get name from other model
154  
-            self.lookup_title = other_model._meta.verbose_name
  150
+        if hasattr(field, 'verbose_name'):
  151
+            self.lookup_title = field.verbose_name
155 152
         else:
156  
-            self.lookup_title = field.verbose_name # use field name
  153
+            self.lookup_title = other_model._meta.verbose_name
157 154
         rel_name = other_model._meta.pk.name
158 155
         self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
159 156
         self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
4  tests/regressiontests/admin_filters/models.py
@@ -5,8 +5,8 @@
5 5
 class Book(models.Model):
6 6
     title = models.CharField(max_length=50)
7 7
     year = models.PositiveIntegerField(null=True, blank=True)
8  
-    author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True)
9  
-    contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)
  8
+    author = models.ForeignKey(User, verbose_name="Verbose Author", related_name='books_authored', blank=True, null=True)
  9
+    contributors = models.ManyToManyField(User, verbose_name="Verbose Contributors", related_name='books_contributed', blank=True, null=True)
10 10
     is_best_seller = models.NullBooleanField(default=0)
11 11
     date_registered = models.DateField(null=True)
12 12
     no = models.IntegerField(verbose_name=u'number', blank=True, null=True) # This field is intentionally 2 characters long. See #16080.
10  tests/regressiontests/admin_filters/tests.py
@@ -244,7 +244,7 @@ def test_relatedfieldlistfilter_foreignkey(self):
244 244
 
245 245
         # Make sure the last choice is None and is selected
246 246
         filterspec = changelist.get_filters(request)[0][1]
247  
-        self.assertEqual(force_unicode(filterspec.title), u'author')
  247
+        self.assertEquals(force_unicode(filterspec.title), u'Verbose Author')
248 248
         choices = list(filterspec.choices(changelist))
249 249
         self.assertEqual(choices[-1]['selected'], True)
250 250
         self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
@@ -254,7 +254,7 @@ def test_relatedfieldlistfilter_foreignkey(self):
254 254
 
255 255
         # Make sure the correct choice is selected
256 256
         filterspec = changelist.get_filters(request)[0][1]
257  
-        self.assertEqual(force_unicode(filterspec.title), u'author')
  257
+        self.assertEquals(force_unicode(filterspec.title), u'Verbose Author')
258 258
         # order of choices depends on User model, which has no order
259 259
         choice = select_by(filterspec.choices(changelist), "display", "alfred")
260 260
         self.assertEqual(choice['selected'], True)
@@ -272,7 +272,7 @@ def test_relatedfieldlistfilter_manytomany(self):
272 272
 
273 273
         # Make sure the last choice is None and is selected
274 274
         filterspec = changelist.get_filters(request)[0][2]
275  
-        self.assertEqual(force_unicode(filterspec.title), u'user')
  275
+        self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors')
276 276
         choices = list(filterspec.choices(changelist))
277 277
         self.assertEqual(choices[-1]['selected'], True)
278 278
         self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
@@ -282,7 +282,7 @@ def test_relatedfieldlistfilter_manytomany(self):
282 282
 
283 283
         # Make sure the correct choice is selected
284 284
         filterspec = changelist.get_filters(request)[0][2]
285  
-        self.assertEqual(force_unicode(filterspec.title), u'user')
  285
+        self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors')
286 286
         choice = select_by(filterspec.choices(changelist), "display", "bob")
287 287
         self.assertEqual(choice['selected'], True)
288 288
         self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk)
@@ -483,7 +483,7 @@ def test_simplelistfilter(self):
483 483
         self.assertEqual(choices[3]['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk)
484 484
 
485 485
         filterspec = changelist.get_filters(request)[0][0]
486  
-        self.assertEqual(force_unicode(filterspec.title), u'author')
  486
+        self.assertEquals(force_unicode(filterspec.title), u'Verbose Author')
487 487
         choice = select_by(filterspec.choices(changelist), "display", "alfred")
488 488
         self.assertEqual(choice['selected'], True)
489 489
         self.assertEqual(choice['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk)

0 notes on commit 93a5814

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