Permalink
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...
brosner committed Jan 9, 2010
1 parent 2dd9a85 commit ac1b275130e54e94fd3bd5b3a5ddc4603a247a27
@@ -281,15 +281,18 @@ 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)
elif isinstance(field, models.DecimalField):
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)
@@ -0,0 +1 @@
# needed for tests
@@ -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)

0 comments on commit ac1b275

Please sign in to comment.