Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #12550 -- better handling with choices and null fields when dis…

…playing read-only values

Thanks Jacques Beaurain for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12148 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ac1b275130e54e94fd3bd5b3a5ddc4603a247a27 1 parent 2dd9a85
@brosner brosner authored
View
9 django/contrib/admin/util.py
@@ -281,7 +281,12 @@ def label_for_field(name, model, model_admin):
def display_for_field(value, field):
from django.contrib.admin.templatetags.admin_list import _boolean_icon
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
- if isinstance(field, models.DateField) or isinstance(field, models.TimeField):
+
+ if field.flatchoices:
+ return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_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)
@@ -289,7 +294,5 @@ def display_for_field(value, field):
return formats.number_format(value, field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
- elif field.flatchoices:
- return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
else:
return smart_unicode(value)
View
0  tests/regressiontests/admin_util/__init__.py
No changes.
View
1  tests/regressiontests/admin_util/models.py
@@ -0,0 +1 @@
+# needed for tests
View
40 tests/regressiontests/admin_util/tests.py
@@ -0,0 +1,40 @@
+import unittest
+
+from django.db import models
+
+from django.contrib import admin
+from django.contrib.admin.util import display_for_field
+from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
+
+
+
+class UtilTests(unittest.TestCase):
+ def test_null_display_for_field(self):
+ """
+ Regression test for #12550: display_for_field should handle None
+ value.
+ """
+ display_value = display_for_field(None, models.CharField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+
+ display_value = display_for_field(None, models.CharField(
+ choices=(
+ (None, "test_none"),
+ )
+ ))
+ self.assertEqual(display_value, "test_none")
+
+ display_value = display_for_field(None, models.DateField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+
+ display_value = display_for_field(None, models.TimeField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+
+ display_value = display_for_field(None, models.NullBooleanField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+
+ display_value = display_for_field(None, models.DecimalField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
+
+ display_value = display_for_field(None, models.FloatField())
+ self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
Please sign in to comment.
Something went wrong with that request. Please try again.