Ticket #15221: 15221_m2m_filterspec_title.2.diff

File 15221_m2m_filterspec_title.2.diff, 4.3 KB (added by Julien Phalip, 14 years ago)

Patch + tests

  • django/contrib/admin/filterspecs.py

    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    index d887af2..f017512 100644
    a b class RelatedFilterSpec(FilterSpec):  
    6868            f, request, params, model, model_admin, field_path=field_path)
    6969
    7070        other_model = get_model_from_relation(f)
    71         if isinstance(f, (models.ManyToManyField,
    72                           models.related.RelatedObject)):
    73             # no direct field on this model, get name from other model
    74             self.lookup_title = other_model._meta.verbose_name
     71        if hasattr(f, 'verbose_name') and f.verbose_name != u'':
     72            self.lookup_title = f.verbose_name
    7573        else:
    76             self.lookup_title = f.verbose_name # use field name
     74            self.lookup_title = other_model._meta.verbose_name
    7775        rel_name = other_model._meta.pk.name
    7876        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
    7977        self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
  • tests/regressiontests/admin_filterspecs/models.py

    diff --git a/tests/regressiontests/admin_filterspecs/models.py b/tests/regressiontests/admin_filterspecs/models.py
    index fe9fdfe..cea88d0 100644
    a b from django.contrib.auth.models import User  
    44class Book(models.Model):
    55    title = models.CharField(max_length=25)
    66    year = models.PositiveIntegerField(null=True, blank=True)
    7     author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True)
    8     contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)
     7    author = models.ForeignKey(User, verbose_name='author_verbose', related_name='books_authored', blank=True, null=True)
     8    contributors = models.ManyToManyField(User, verbose_name='contributors_verbose', related_name='books_contributed', blank=True, null=True)
    99
    1010    def __unicode__(self):
    1111        return self.title
  • tests/regressiontests/admin_filterspecs/tests.py

    diff --git a/tests/regressiontests/admin_filterspecs/tests.py b/tests/regressiontests/admin_filterspecs/tests.py
    index 64a6ac7..e4b1bfe 100644
    a b class FilterSpecsTests(TestCase):  
    7070
    7171        # Make sure the last choice is None and is selected
    7272        filterspec = changelist.get_filters(request)[0][1]
    73         self.assertEquals(force_unicode(filterspec.title()), u'author')
     73        self.assertEquals(force_unicode(filterspec.title()), u'author_verbose')
    7474        choices = list(filterspec.choices(changelist))
    7575        self.assertEquals(choices[-1]['selected'], True)
    7676        self.assertEquals(choices[-1]['query_string'], '?author__isnull=True')
    class FilterSpecsTests(TestCase):  
    8080
    8181        # Make sure the correct choice is selected
    8282        filterspec = changelist.get_filters(request)[0][1]
    83         self.assertEquals(force_unicode(filterspec.title()), u'author')
     83        self.assertEquals(force_unicode(filterspec.title()), u'author_verbose')
    8484        choices = list(filterspec.choices(changelist))
    8585        self.assertEquals(choices[1]['selected'], True)
    8686        self.assertEquals(choices[1]['query_string'], '?author__id__exact=1')
    class FilterSpecsTests(TestCase):  
    9696
    9797        # Make sure the last choice is None and is selected
    9898        filterspec = changelist.get_filters(request)[0][2]
    99         self.assertEquals(force_unicode(filterspec.title()), u'user')
     99        self.assertEquals(force_unicode(filterspec.title()), u'contributors_verbose')
    100100        choices = list(filterspec.choices(changelist))
    101101        self.assertEquals(choices[-1]['selected'], True)
    102102        self.assertEquals(choices[-1]['query_string'], '?contributors__isnull=True')
    class FilterSpecsTests(TestCase):  
    106106
    107107        # Make sure the correct choice is selected
    108108        filterspec = changelist.get_filters(request)[0][2]
    109         self.assertEquals(force_unicode(filterspec.title()), u'user')
     109        self.assertEquals(force_unicode(filterspec.title()), u'contributors_verbose')
    110110        choices = list(filterspec.choices(changelist))
    111111        self.assertEquals(choices[2]['selected'], True)
    112112        self.assertEquals(choices[2]['query_string'], '?contributors__id__exact=2')
Back to Top