Ticket #9954: t9954-r9698.diff

File t9954-r9698.diff, 3.1 KB (added by ramiro, 7 years ago)
  • django/contrib/admin/filterspecs.py

    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    a b  
    8989        yield {'selected': self.lookup_val is None,
    9090               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
    9191               'display': _('All')}
    92         for k, v in self.field.choices:
     92        for k, v in self.field.flatchoices:
    9393            yield {'selected': smart_unicode(k) == self.lookup_val,
    9494                    'query_string': cl.get_query_string({self.lookup_kwarg: k}),
    9595                    'display': v}
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    a b  
    134134class ThingAdmin(admin.ModelAdmin):
    135135    list_filter = ('color',)
    136136
     137class Fabric(models.Model):
     138    NG_CHOICES = (
     139        ('Textured', (
     140                ('x', 'Horizontal'),
     141                ('y', 'Vertical'),
     142            )
     143        ),
     144        ('plain', 'Smooth'),
     145    )
     146    surface = models.CharField(max_length=20, choices=NG_CHOICES)
     147
     148class FabricAdmin(admin.ModelAdmin):
     149    list_filter = ('surface',)
     150
    137151admin.site.register(Article, ArticleAdmin)
    138152admin.site.register(CustomArticle, CustomArticleAdmin)
    139153admin.site.register(Section, inlines=[ArticleInline])
    140154admin.site.register(ModelWithStringPrimaryKey)
    141155admin.site.register(Color)
    142156admin.site.register(Thing, ThingAdmin)
     157admin.site.register(Fabric, FabricAdmin)
    143158
    144159# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    145160# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    a b  
    160160            '<a href="?color__id__exact=3">Blue</a>' in response.content,
    161161            "Changelist filter not correctly limited by limit_choices_to."
    162162        )
    163        
     163
     164    def testNamedGroupFieldChoicesFilter(self):
     165        """
     166        Ensures the filter UI shows correctly when at least one named group has
     167        been used in the choices option of a model field.
     168        """
     169        response = self.client.get('/test_admin/admin/admin_views/fabric/')
     170        self.failUnlessEqual(response.status_code, 200)
     171        self.failUnless(
     172            '<div id="changelist-filter">' in response.content,
     173            "Expected filter not found in changelist view."
     174        )
     175        self.failUnless(
     176            '<a href="?surface__exact=x">Horizontal</a>' in response.content and
     177            '<a href="?surface__exact=y">Vertical</a>' in response.content,
     178            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
     179        )
     180
    164181    def testIncorrectLookupParameters(self):
    165182        """Ensure incorrect lookup parameters are handled gracefully."""
    166183        response = self.client.get('/test_admin/admin/admin_views/thing/', {'notarealfield': '5'})
Back to Top