Author: ubernostrum
Date: 2010-03-10 02:37:17 -0600 (Wed, 10 Mar 2010)
New Revision: 12746

Modified:
   django/trunk/django/contrib/admin/util.py
   django/trunk/tests/regressiontests/admin_util/tests.py
   django/trunk/tests/regressiontests/admin_views/models.py
   django/trunk/tests/regressiontests/admin_views/tests.py
Log:
Ensure that NullBooleanField displays the appropriate icon for null values in 
admin changelists. Refs #13071.

Modified: django/trunk/django/contrib/admin/util.py
===================================================================
--- django/trunk/django/contrib/admin/util.py   2010-03-10 07:42:25 UTC (rev 
12745)
+++ django/trunk/django/contrib/admin/util.py   2010-03-10 08:37:17 UTC (rev 
12746)
@@ -337,12 +337,14 @@
 
     if field.flatchoices:
         return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
+    # NullBooleanField needs special-case null-handling, so it comes
+    # before the general null test.
+    elif isinstance(field, models.BooleanField) or isinstance(field, 
models.NullBooleanField):
+        return _boolean_icon(value)
     elif value is None:
         return EMPTY_CHANGELIST_VALUE
     elif isinstance(field, models.DateField) or isinstance(field, 
models.TimeField):
         return formats.localize(value)
-    elif isinstance(field, models.BooleanField) or isinstance(field, 
models.NullBooleanField):
-        return _boolean_icon(value)
     elif isinstance(field, models.DecimalField):
         return formats.number_format(value, field.decimal_places)
     elif isinstance(field, models.FloatField):

Modified: django/trunk/tests/regressiontests/admin_util/tests.py
===================================================================
--- django/trunk/tests/regressiontests/admin_util/tests.py      2010-03-10 
07:42:25 UTC (rev 12745)
+++ django/trunk/tests/regressiontests/admin_util/tests.py      2010-03-10 
08:37:17 UTC (rev 12746)
@@ -1,6 +1,7 @@
 from datetime import datetime
 import unittest
 
+from django.conf import settings
 from django.db import models
 from django.utils.formats import localize
 from django.test import TestCase
@@ -131,8 +132,11 @@
         display_value = display_for_field(None, models.TimeField())
         self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
 
+        # Regression test for #13071: NullBooleanField has special
+        # handling.
         display_value = display_for_field(None, models.NullBooleanField())
-        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+        expected = u'<img src="%simg/admin/icon-unknown.gif" alt="None" />' % 
settings.ADMIN_MEDIA_PREFIX
+        self.assertEqual(display_value, expected)
 
         display_value = display_for_field(None, models.DecimalField())
         self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)

Modified: django/trunk/tests/regressiontests/admin_views/models.py
===================================================================
--- django/trunk/tests/regressiontests/admin_views/models.py    2010-03-10 
07:42:25 UTC (rev 12745)
+++ django/trunk/tests/regressiontests/admin_views/models.py    2010-03-10 
08:37:17 UTC (rev 12746)
@@ -464,11 +464,13 @@
     title = models.CharField(max_length=100)
     content = models.TextField()
     posted = models.DateField(default=datetime.date.today)
+    public = models.NullBooleanField()
 
     def awesomeness_level(self):
         return "Very awesome."
 
 class PostAdmin(admin.ModelAdmin):
+    list_display = ['title', 'public']
     readonly_fields = ('posted', 'awesomeness_level', 'coolness', lambda obj: 
"foo")
 
     inlines = [

Modified: django/trunk/tests/regressiontests/admin_views/tests.py
===================================================================
--- django/trunk/tests/regressiontests/admin_views/tests.py     2010-03-10 
07:42:25 UTC (rev 12745)
+++ django/trunk/tests/regressiontests/admin_views/tests.py     2010-03-10 
08:37:17 UTC (rev 12746)
@@ -253,6 +253,15 @@
             "Changelist filter isn't showing options contained inside a model 
field 'choices' option named group."
         )
 
+    def testChangeListNullBooleanDisplay(self):
+        Post.objects.create(public=None)
+        # This hard-codes the URl because it'll fail if it runs
+        # against the 'admin2' custom admin (which doesn't have the
+        # Post model).
+        response = self.client.get("/test_admin/admin/admin_views/post/")
+        self.failUnless('icon-unknown.gif' in response.content)
+        print "Passed"
+
 class SaveAsTests(TestCase):
     fixtures = ['admin-views-users.xml','admin-views-person.xml']
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to