Skip to content

Commit

Permalink
Fixed #15221 -- Made the admin filters on foreign key and m2m relatio…
Browse files Browse the repository at this point in the history
…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
jphalip committed Oct 16, 2011
1 parent c582609 commit 93a5814
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
9 changes: 3 additions & 6 deletions django/contrib/admin/filters.py
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/regressiontests/admin_filters/models.py
Expand Up @@ -5,8 +5,8 @@
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.
Expand Down
10 changes: 5 additions & 5 deletions tests/regressiontests/admin_filters/tests.py
Expand Up @@ -244,7 +244,7 @@ def test_relatedfieldlistfilter_foreignkey(self):

# 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')
Expand All @@ -254,7 +254,7 @@ def test_relatedfieldlistfilter_foreignkey(self):

# 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)
Expand All @@ -272,7 +272,7 @@ def test_relatedfieldlistfilter_manytomany(self):

# 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')
Expand All @@ -282,7 +282,7 @@ def test_relatedfieldlistfilter_manytomany(self):

# 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)
Expand Down Expand Up @@ -483,7 +483,7 @@ def test_simplelistfilter(self):
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)
Expand Down

0 comments on commit 93a5814

Please sign in to comment.