Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Brian Rosner authored January 09, 2010
9  django/contrib/admin/util.py
@@ -281,7 +281,12 @@ def label_for_field(name, model, model_admin):
281 281
 def display_for_field(value, field):
282 282
     from django.contrib.admin.templatetags.admin_list import _boolean_icon
283 283
     from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
284  
-    if isinstance(field, models.DateField) or isinstance(field, models.TimeField):
  284
+
  285
+    if field.flatchoices:
  286
+        return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
  287
+    elif value is None:
  288
+        return EMPTY_CHANGELIST_VALUE
  289
+    elif isinstance(field, models.DateField) or isinstance(field, models.TimeField):
285 290
         return formats.localize(value)
286 291
     elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
287 292
         return _boolean_icon(value)
@@ -289,7 +294,5 @@ def display_for_field(value, field):
289 294
         return formats.number_format(value, field.decimal_places)
290 295
     elif isinstance(field, models.FloatField):
291 296
         return formats.number_format(value)
292  
-    elif field.flatchoices:
293  
-        return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
294 297
     else:
295 298
         return smart_unicode(value)
0  tests/regressiontests/admin_util/__init__.py
No changes.
1  tests/regressiontests/admin_util/models.py
... ...
@@ -0,0 +1 @@
  1
+# needed for tests
40  tests/regressiontests/admin_util/tests.py
... ...
@@ -0,0 +1,40 @@
  1
+import unittest
  2
+
  3
+from django.db import models
  4
+
  5
+from django.contrib import admin
  6
+from django.contrib.admin.util import display_for_field
  7
+from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
  8
+
  9
+
  10
+
  11
+class UtilTests(unittest.TestCase):
  12
+    def test_null_display_for_field(self):
  13
+        """
  14
+        Regression test for #12550: display_for_field should handle None
  15
+        value.
  16
+        """
  17
+        display_value = display_for_field(None, models.CharField())
  18
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  19
+
  20
+        display_value = display_for_field(None, models.CharField(
  21
+            choices=(
  22
+                (None, "test_none"),
  23
+            )
  24
+        ))
  25
+        self.assertEqual(display_value, "test_none")
  26
+
  27
+        display_value = display_for_field(None, models.DateField())
  28
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  29
+
  30
+        display_value = display_for_field(None, models.TimeField())
  31
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  32
+
  33
+        display_value = display_for_field(None, models.NullBooleanField())
  34
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  35
+
  36
+        display_value = display_for_field(None, models.DecimalField())
  37
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  38
+
  39
+        display_value = display_for_field(None, models.FloatField())
  40
+        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)

0 notes on commit ac1b275

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