From ac1b275130e54e94fd3bd5b3a5ddc4603a247a27 Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Sat, 9 Jan 2010 21:28:54 +0000 Subject: [PATCH] Fixed #12550 -- better handling with choices and null fields when displaying 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 --- django/contrib/admin/util.py | 9 +++-- tests/regressiontests/admin_util/__init__.py | 0 tests/regressiontests/admin_util/models.py | 1 + tests/regressiontests/admin_util/tests.py | 40 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/regressiontests/admin_util/__init__.py create mode 100644 tests/regressiontests/admin_util/models.py create mode 100644 tests/regressiontests/admin_util/tests.py diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index 4a9f49cb7c..dc7ebcbb91 100644 --- a/django/contrib/admin/util.py +++ b/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) diff --git a/tests/regressiontests/admin_util/__init__.py b/tests/regressiontests/admin_util/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/admin_util/models.py b/tests/regressiontests/admin_util/models.py new file mode 100644 index 0000000000..b40f364b76 --- /dev/null +++ b/tests/regressiontests/admin_util/models.py @@ -0,0 +1 @@ +# needed for tests \ No newline at end of file diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py new file mode 100644 index 0000000000..11e71e20ec --- /dev/null +++ b/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)