From 93a581431a493febf173a8a3f2bd27ab450f228b Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Sun, 16 Oct 2011 06:42:50 +0000 Subject: [PATCH] Fixed #15221 -- Made the admin filters on foreign key and m2m relationships 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 --- django/contrib/admin/filters.py | 9 +++------ tests/regressiontests/admin_filters/models.py | 4 ++-- tests/regressiontests/admin_filters/tests.py | 10 +++++----- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index 261ba7a02a..ed98a9ebb5 100644 --- a/django/contrib/admin/filters.py +++ b/django/contrib/admin/filters.py @@ -146,14 +146,11 @@ class RelatedFieldListFilter(FieldListFilter): def __init__(self, field, request, params, model, model_admin, field_path): super(RelatedFieldListFilter, self).__init__( field, request, params, model, model_admin, field_path) - other_model = get_model_from_relation(field) - if isinstance(field, (models.ManyToManyField, - models.related.RelatedObject)): - # no direct field on this model, get name from other model - self.lookup_title = other_model._meta.verbose_name + if hasattr(field, 'verbose_name'): + self.lookup_title = field.verbose_name else: - self.lookup_title = field.verbose_name # use field name + self.lookup_title = other_model._meta.verbose_name rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path) diff --git a/tests/regressiontests/admin_filters/models.py b/tests/regressiontests/admin_filters/models.py index ac43263d2e..2fa6e6631d 100644 --- a/tests/regressiontests/admin_filters/models.py +++ b/tests/regressiontests/admin_filters/models.py @@ -5,8 +5,8 @@ from django.db import models class Book(models.Model): title = models.CharField(max_length=50) year = models.PositiveIntegerField(null=True, blank=True) - author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True) - contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True) + author = models.ForeignKey(User, verbose_name="Verbose Author", related_name='books_authored', blank=True, null=True) + contributors = models.ManyToManyField(User, verbose_name="Verbose Contributors", related_name='books_contributed', blank=True, null=True) is_best_seller = models.NullBooleanField(default=0) date_registered = models.DateField(null=True) no = models.IntegerField(verbose_name=u'number', blank=True, null=True) # This field is intentionally 2 characters long. See #16080. diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index e276e55a35..28693aef98 100644 --- a/tests/regressiontests/admin_filters/tests.py +++ b/tests/regressiontests/admin_filters/tests.py @@ -244,7 +244,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), u'author') + self.assertEquals(force_unicode(filterspec.title), u'Verbose Author') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?author__isnull=True') @@ -254,7 +254,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][1] - self.assertEqual(force_unicode(filterspec.title), u'author') + self.assertEquals(force_unicode(filterspec.title), u'Verbose Author') # order of choices depends on User model, which has no order choice = select_by(filterspec.choices(changelist), "display", "alfred") self.assertEqual(choice['selected'], True) @@ -272,7 +272,7 @@ class ListFiltersTests(TestCase): # Make sure the last choice is None and is selected filterspec = changelist.get_filters(request)[0][2] - self.assertEqual(force_unicode(filterspec.title), u'user') + self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors') choices = list(filterspec.choices(changelist)) self.assertEqual(choices[-1]['selected'], True) self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True') @@ -282,7 +282,7 @@ class ListFiltersTests(TestCase): # Make sure the correct choice is selected filterspec = changelist.get_filters(request)[0][2] - self.assertEqual(force_unicode(filterspec.title), u'user') + self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors') choice = select_by(filterspec.choices(changelist), "display", "bob") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk) @@ -483,7 +483,7 @@ class ListFiltersTests(TestCase): self.assertEqual(choices[3]['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk) filterspec = changelist.get_filters(request)[0][0] - self.assertEqual(force_unicode(filterspec.title), u'author') + self.assertEquals(force_unicode(filterspec.title), u'Verbose Author') choice = select_by(filterspec.choices(changelist), "display", "alfred") self.assertEqual(choice['selected'], True) self.assertEqual(choice['query_string'], '?publication-decade=the+00s&author__id__exact=%s' % self.alfred.pk)