From ee17cabba0e1f2cbb85b2044eda43108e3f4371c Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 1 Apr 2009 14:14:20 +0000 Subject: Fixed #9969: choices with options groups (added in [7977]) now work correctly in the admin with list_display and list_filter. Thanks, ramiro. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10318 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/admin_views/customadmin.py | 1 + .../admin_views/fixtures/admin-views-fabrics.xml | 12 ++++++++ tests/regressiontests/admin_views/models.py | 16 +++++++++++ tests/regressiontests/admin_views/tests.py | 33 +++++++++++++++++++++- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml (limited to 'tests') diff --git a/tests/regressiontests/admin_views/customadmin.py b/tests/regressiontests/admin_views/customadmin.py index c812eab98b..70e87ebcfe 100644 --- a/tests/regressiontests/admin_views/customadmin.py +++ b/tests/regressiontests/admin_views/customadmin.py @@ -28,3 +28,4 @@ site = Admin2(name="admin2") site.register(models.Article, models.ArticleAdmin) site.register(models.Section, inlines=[models.ArticleInline]) site.register(models.Thing, models.ThingAdmin) +site.register(models.Fabric, models.FabricAdmin) 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 e2d087db42..df533f82eb 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -135,6 +135,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 Person(models.Model): GENDER_CHOICES = ( (1, "Male"), @@ -283,6 +298,7 @@ admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin) admin.site.register(Podcast, PodcastAdmin) 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 2e228743da..cecd797e85 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -21,7 +21,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'] # Store the bit of the URL where the admin is registered as a class # variable. That way we can test a second AdminSite just by subclassing @@ -182,6 +182,37 @@ class AdminViewBasicTest(TestCase): response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'}) self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit) + 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/%s/admin_views/fabric/' % self.urlbit) + 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/%s/admin_views/fabric/' % self.urlbit) + 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." + ) + class CustomModelAdminTest(AdminViewBasicTest): urlbit = "admin2" -- cgit v1.3