Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Ensure that NullBooleanField displays the appropriate icon for null v…

…alues in admin changelists. Refs #13071.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12746 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a0c77ada990ca1e61b37d7ecdcf533e88c11a510 1 parent baa4d3b
James Bennett authored March 10, 2010
6  django/contrib/admin/util.py
@@ -337,12 +337,14 @@ def display_for_field(value, field):
337 337
 
338 338
     if field.flatchoices:
339 339
         return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
  340
+    # NullBooleanField needs special-case null-handling, so it comes
  341
+    # before the general null test.
  342
+    elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
  343
+        return _boolean_icon(value)
340 344
     elif value is None:
341 345
         return EMPTY_CHANGELIST_VALUE
342 346
     elif isinstance(field, models.DateField) or isinstance(field, models.TimeField):
343 347
         return formats.localize(value)
344  
-    elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
345  
-        return _boolean_icon(value)
346 348
     elif isinstance(field, models.DecimalField):
347 349
         return formats.number_format(value, field.decimal_places)
348 350
     elif isinstance(field, models.FloatField):
6  tests/regressiontests/admin_util/tests.py
... ...
@@ -1,6 +1,7 @@
1 1
 from datetime import datetime
2 2
 import unittest
3 3
 
  4
+from django.conf import settings
4 5
 from django.db import models
5 6
 from django.utils.formats import localize
6 7
 from django.test import TestCase
@@ -131,8 +132,11 @@ def test_null_display_for_field(self):
131 132
         display_value = display_for_field(None, models.TimeField())
132 133
         self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
133 134
 
  135
+        # Regression test for #13071: NullBooleanField has special
  136
+        # handling.
134 137
         display_value = display_for_field(None, models.NullBooleanField())
135  
-        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  138
+        expected = u'<img src="%simg/admin/icon-unknown.gif" alt="None" />' % settings.ADMIN_MEDIA_PREFIX
  139
+        self.assertEqual(display_value, expected)
136 140
 
137 141
         display_value = display_for_field(None, models.DecimalField())
138 142
         self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
2  tests/regressiontests/admin_views/models.py
@@ -464,11 +464,13 @@ class Post(models.Model):
464 464
     title = models.CharField(max_length=100)
465 465
     content = models.TextField()
466 466
     posted = models.DateField(default=datetime.date.today)
  467
+    public = models.NullBooleanField()
467 468
 
468 469
     def awesomeness_level(self):
469 470
         return "Very awesome."
470 471
 
471 472
 class PostAdmin(admin.ModelAdmin):
  473
+    list_display = ['title', 'public']
472 474
     readonly_fields = ('posted', 'awesomeness_level', 'coolness', lambda obj: "foo")
473 475
 
474 476
     inlines = [
9  tests/regressiontests/admin_views/tests.py
@@ -253,6 +253,15 @@ def testNamedGroupFieldChoicesFilter(self):
253 253
             "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
254 254
         )
255 255
 
  256
+    def testChangeListNullBooleanDisplay(self):
  257
+        Post.objects.create(public=None)
  258
+        # This hard-codes the URl because it'll fail if it runs
  259
+        # against the 'admin2' custom admin (which doesn't have the
  260
+        # Post model).
  261
+        response = self.client.get("/test_admin/admin/admin_views/post/")
  262
+        self.failUnless('icon-unknown.gif' in response.content)
  263
+        print "Passed"
  264
+
256 265
 class SaveAsTests(TestCase):
257 266
     fixtures = ['admin-views-users.xml','admin-views-person.xml']
258 267
 

0 notes on commit a0c77ad

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