Code

Ticket #15221: 15221_m2m_filterspec_title.2.diff

File 15221_m2m_filterspec_title.2.diff, 4.3 KB (added by julien, 3 years ago)

Patch + tests

Line 
1diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
2index d887af2..f017512 100644
3--- a/django/contrib/admin/filterspecs.py
4+++ b/django/contrib/admin/filterspecs.py
5@@ -68,12 +68,10 @@ class RelatedFilterSpec(FilterSpec):
6             f, request, params, model, model_admin, field_path=field_path)
7 
8         other_model = get_model_from_relation(f)
9-        if isinstance(f, (models.ManyToManyField,
10-                          models.related.RelatedObject)):
11-            # no direct field on this model, get name from other model
12-            self.lookup_title = other_model._meta.verbose_name
13+        if hasattr(f, 'verbose_name') and f.verbose_name != u'':
14+            self.lookup_title = f.verbose_name
15         else:
16-            self.lookup_title = f.verbose_name # use field name
17+            self.lookup_title = other_model._meta.verbose_name
18         rel_name = other_model._meta.pk.name
19         self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
20         self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
21diff --git a/tests/regressiontests/admin_filterspecs/models.py b/tests/regressiontests/admin_filterspecs/models.py
22index fe9fdfe..cea88d0 100644
23--- a/tests/regressiontests/admin_filterspecs/models.py
24+++ b/tests/regressiontests/admin_filterspecs/models.py
25@@ -4,8 +4,8 @@ from django.contrib.auth.models import User
26 class Book(models.Model):
27     title = models.CharField(max_length=25)
28     year = models.PositiveIntegerField(null=True, blank=True)
29-    author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True)
30-    contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)
31+    author = models.ForeignKey(User, verbose_name='author_verbose', related_name='books_authored', blank=True, null=True)
32+    contributors = models.ManyToManyField(User, verbose_name='contributors_verbose', related_name='books_contributed', blank=True, null=True)
33 
34     def __unicode__(self):
35         return self.title
36diff --git a/tests/regressiontests/admin_filterspecs/tests.py b/tests/regressiontests/admin_filterspecs/tests.py
37index 64a6ac7..e4b1bfe 100644
38--- a/tests/regressiontests/admin_filterspecs/tests.py
39+++ b/tests/regressiontests/admin_filterspecs/tests.py
40@@ -70,7 +70,7 @@ class FilterSpecsTests(TestCase):
41 
42         # Make sure the last choice is None and is selected
43         filterspec = changelist.get_filters(request)[0][1]
44-        self.assertEquals(force_unicode(filterspec.title()), u'author')
45+        self.assertEquals(force_unicode(filterspec.title()), u'author_verbose')
46         choices = list(filterspec.choices(changelist))
47         self.assertEquals(choices[-1]['selected'], True)
48         self.assertEquals(choices[-1]['query_string'], '?author__isnull=True')
49@@ -80,7 +80,7 @@ class FilterSpecsTests(TestCase):
50 
51         # Make sure the correct choice is selected
52         filterspec = changelist.get_filters(request)[0][1]
53-        self.assertEquals(force_unicode(filterspec.title()), u'author')
54+        self.assertEquals(force_unicode(filterspec.title()), u'author_verbose')
55         choices = list(filterspec.choices(changelist))
56         self.assertEquals(choices[1]['selected'], True)
57         self.assertEquals(choices[1]['query_string'], '?author__id__exact=1')
58@@ -96,7 +96,7 @@ class FilterSpecsTests(TestCase):
59 
60         # Make sure the last choice is None and is selected
61         filterspec = changelist.get_filters(request)[0][2]
62-        self.assertEquals(force_unicode(filterspec.title()), u'user')
63+        self.assertEquals(force_unicode(filterspec.title()), u'contributors_verbose')
64         choices = list(filterspec.choices(changelist))
65         self.assertEquals(choices[-1]['selected'], True)
66         self.assertEquals(choices[-1]['query_string'], '?contributors__isnull=True')
67@@ -106,7 +106,7 @@ class FilterSpecsTests(TestCase):
68 
69         # Make sure the correct choice is selected
70         filterspec = changelist.get_filters(request)[0][2]
71-        self.assertEquals(force_unicode(filterspec.title()), u'user')
72+        self.assertEquals(force_unicode(filterspec.title()), u'contributors_verbose')
73         choices = list(filterspec.choices(changelist))
74         self.assertEquals(choices[2]['selected'], True)
75         self.assertEquals(choices[2]['query_string'], '?contributors__id__exact=2')