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
This commit is contained in:
parent
2dd9a85819
commit
ac1b275130
|
@ -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)
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue