Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9969: choices with options groups (added in [7977]) now work c…

…orrectly 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
  • Loading branch information...
commit ee17cabba0e1f2cbb85b2044eda43108e3f4371c 1 parent a2dec37
Jacob Kaplan-Moss authored April 01, 2009
2  django/contrib/admin/filterspecs.py
@@ -90,7 +90,7 @@ def choices(self, cl):
90 90
         yield {'selected': self.lookup_val is None,
91 91
                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
92 92
                'display': _('All')}
93  
-        for k, v in self.field.choices:
  93
+        for k, v in self.field.flatchoices:
94 94
             yield {'selected': smart_unicode(k) == self.lookup_val,
95 95
                     'query_string': cl.get_query_string({self.lookup_kwarg: k}),
96 96
                     'display': v}
4  django/contrib/admin/templatetags/admin_list.py
@@ -205,8 +205,8 @@ def items_for_result(cl, result, form):
205 205
                     result_repr = EMPTY_CHANGELIST_VALUE
206 206
             # Fields with choices are special: Use the representation
207 207
             # of the choice.
208  
-            elif f.choices:
209  
-                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
  208
+            elif f.flatchoices:
  209
+                result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE)
210 210
             else:
211 211
                 result_repr = escape(field_val)
212 212
         if force_unicode(result_repr) == '':
1  tests/regressiontests/admin_views/customadmin.py
@@ -28,3 +28,4 @@ def my_view(self, request):
28 28
 site.register(models.Article, models.ArticleAdmin)
29 29
 site.register(models.Section, inlines=[models.ArticleInline])
30 30
 site.register(models.Thing, models.ThingAdmin)
  31
+site.register(models.Fabric, models.FabricAdmin)
12  tests/regressiontests/admin_views/fixtures/admin-views-fabrics.xml
... ...
@@ -0,0 +1,12 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<django-objects version="1.0">
  3
+  <object pk="1" model="admin_views.fabric">
  4
+    <field type="CharField" name="surface">x</field>
  5
+  </object>
  6
+  <object pk="2" model="admin_views.fabric">
  7
+    <field type="CharField" name="surface">y</field>
  8
+  </object>
  9
+  <object pk="3" model="admin_views.fabric">
  10
+    <field type="CharField" name="surface">plain</field>
  11
+  </object>
  12
+</django-objects>
16  tests/regressiontests/admin_views/models.py
@@ -135,6 +135,21 @@ def __unicode__(self):
135 135
 class ThingAdmin(admin.ModelAdmin):
136 136
     list_filter = ('color',)
137 137
 
  138
+class Fabric(models.Model):
  139
+    NG_CHOICES = (
  140
+        ('Textured', (
  141
+                ('x', 'Horizontal'),
  142
+                ('y', 'Vertical'),
  143
+            )
  144
+        ),
  145
+        ('plain', 'Smooth'),
  146
+    )
  147
+    surface = models.CharField(max_length=20, choices=NG_CHOICES)
  148
+
  149
+class FabricAdmin(admin.ModelAdmin):
  150
+    list_display = ('surface',)
  151
+    list_filter = ('surface',)
  152
+
138 153
 class Person(models.Model):
139 154
     GENDER_CHOICES = (
140 155
         (1, "Male"),
@@ -283,6 +298,7 @@ def queryset(self, request):
283 298
 admin.site.register(Podcast, PodcastAdmin)
284 299
 admin.site.register(Parent, ParentAdmin)
285 300
 admin.site.register(EmptyModel, EmptyModelAdmin)
  301
+admin.site.register(Fabric, FabricAdmin)
286 302
 
287 303
 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
288 304
 # That way we cover all four cases:
33  tests/regressiontests/admin_views/tests.py
@@ -21,7 +21,7 @@
21 21
     from sets import Set as set
22 22
 
23 23
 class AdminViewBasicTest(TestCase):
24  
-    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml']
  24
+    fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
25 25
 
26 26
     # Store the bit of the URL where the admin is registered as a class
27 27
     # variable. That way we can test a second AdminSite just by subclassing
@@ -182,6 +182,37 @@ def testIncorrectLookupParameters(self):
182 182
         response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
183 183
         self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
184 184
 
  185
+    def testNamedGroupFieldChoicesChangeList(self):
  186
+        """
  187
+        Ensures the admin changelist shows correct values in the relevant column
  188
+        for rows corresponding to instances of a model in which a named group
  189
+        has been used in the choices option of a field.
  190
+        """
  191
+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
  192
+        self.failUnlessEqual(response.status_code, 200)
  193
+        self.failUnless(
  194
+            '<a href="1/">Horizontal</a>' in response.content and
  195
+            '<a href="2/">Vertical</a>' in response.content,
  196
+            "Changelist table isn't showing the right human-readable values set by a model field 'choices' option named group."
  197
+        )
  198
+
  199
+    def testNamedGroupFieldChoicesFilter(self):
  200
+        """
  201
+        Ensures the filter UI shows correctly when at least one named group has
  202
+        been used in the choices option of a model field.
  203
+        """
  204
+        response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
  205
+        self.failUnlessEqual(response.status_code, 200)
  206
+        self.failUnless(
  207
+            '<div id="changelist-filter">' in response.content,
  208
+            "Expected filter not found in changelist view."
  209
+        )
  210
+        self.failUnless(
  211
+            '<a href="?surface__exact=x">Horizontal</a>' in response.content and
  212
+            '<a href="?surface__exact=y">Vertical</a>' in response.content,
  213
+            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
  214
+        )
  215
+
185 216
 class CustomModelAdminTest(AdminViewBasicTest):
186 217
     urlbit = "admin2"
187 218
 

0 notes on commit ee17cab

Please sign in to comment.
Something went wrong with that request. Please try again.