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
This commit is contained in:
parent
c582609c8d
commit
93a581431a
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue