diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py index fd258df6b7..6f643ee102 100644 --- a/django/contrib/admin/filterspecs.py +++ b/django/contrib/admin/filterspecs.py @@ -90,7 +90,7 @@ class ChoicesFilterSpec(FilterSpec): yield {'selected': self.lookup_val is None, 'query_string': cl.get_query_string({}, [self.lookup_kwarg]), 'display': _('All')} - for k, v in self.field.choices: + for k, v in self.field.flatchoices: yield {'selected': smart_unicode(k) == self.lookup_val, 'query_string': cl.get_query_string({self.lookup_kwarg: k}), 'display': v} diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 37cdb91c3c..c19b745300 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -205,8 +205,8 @@ def items_for_result(cl, result): result_repr = EMPTY_CHANGELIST_VALUE # Fields with choices are special: Use the representation # of the choice. - elif f.choices: - result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE) + elif f.flatchoices: + result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': diff --git a/tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml b/tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml new file mode 100644 index 0000000000..485bb27c2a --- /dev/null +++ b/tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml @@ -0,0 +1,12 @@ + + + + x + + + y + + + plain + + diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index fbfe59f97e..d9ac372da0 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -134,6 +134,21 @@ class Thing(models.Model): class ThingAdmin(admin.ModelAdmin): list_filter = ('color',) +class Fabric(models.Model): + NG_CHOICES = ( + ('Textured', ( + ('x', 'Horizontal'), + ('y', 'Vertical'), + ) + ), + ('plain', 'Smooth'), + ) + surface = models.CharField(max_length=20, choices=NG_CHOICES) + +class FabricAdmin(admin.ModelAdmin): + list_display = ('surface',) + list_filter = ('surface',) + class Persona(models.Model): """ A simple persona associated with accounts, to test inlining of related @@ -208,6 +223,7 @@ admin.site.register(Thing, ThingAdmin) admin.site.register(Persona, PersonaAdmin) admin.site.register(Parent, ParentAdmin) admin.site.register(EmptyModel, EmptyModelAdmin) +admin.site.register(Fabric, FabricAdmin) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 4db15fc14c..e1a23da37e 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -19,7 +19,7 @@ except NameError: from sets import Set as set class AdminViewBasicTest(TestCase): - fixtures = ['admin-views-users.xml', 'admin-views-colors.xml'] + fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml'] def setUp(self): self.client.login(username='super', password='secret') @@ -175,6 +175,37 @@ class AdminViewBasicTest(TestCase): response = self.client.get('/test_admin/admin/admin_views/thing/', {'color__id__exact': 'StringNotInteger!'}) self.assertRedirects(response, '/test_admin/admin/admin_views/thing/?e=1') + def testNamedGroupFieldChoicesChangeList(self): + """ + Ensures the admin changelist shows correct values in the relevant column + for rows corresponding to instances of a model in which a named group + has been used in the choices option of a field. + """ + response = self.client.get('/test_admin/admin/admin_views/fabric/') + self.failUnlessEqual(response.status_code, 200) + self.failUnless( + 'Horizontal' in response.content and + 'Vertical' in response.content, + "Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group." + ) + + def testNamedGroupFieldChoicesFilter(self): + """ + Ensures the filter UI shows correctly when at least one named group has + been used in the choices option of a model field. + """ + response = self.client.get('/test_admin/admin/admin_views/fabric/') + self.failUnlessEqual(response.status_code, 200) + self.failUnless( + '
' in response.content, + "Expected filter not found in changelist view." + ) + self.failUnless( + 'Horizontal' in response.content and + 'Vertical' in response.content, + "Changelist filter isn't showing options contained inside a model field 'choices' option named group." + ) + def get_perm(Model, perm): """Return the permission object, for the Model""" ct = ContentType.objects.get_for_model(Model)