diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index 4a9f49cb7ce..dc7ebcbb915 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 00000000000..e69de29bb2d diff --git a/tests/regressiontests/admin_util/models.py b/tests/regressiontests/admin_util/models.py new file mode 100644 index 00000000000..b40f364b761 --- /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 00000000000..11e71e20ec9 --- /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)