Ticket #12550: django_nullvalue_adminlist_display.2.patch
File django_nullvalue_adminlist_display.2.patch, 3.4 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/util.py
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 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) -
tests/regressiontests/admin_util/models.py
1 from django.db import models 2 3 4 No newline at end of file -
tests/regressiontests/admin_util/tests.py
1 import unittest 2 from django.contrib import admin 3 from django.db import models 4 from django.contrib.admin.util import display_for_field 5 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE 6 7 class UtilTests(unittest.TestCase): 8 def test_null_display_for_field(self): 9 """ 10 Regression test for #12550: display_for_field should not throw 11 if None value is passed. 12 """ 13 charfield = models.CharField() 14 15 display_value = display_for_field(None, models.CharField()) 16 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 17 18 display_value = display_for_field(None, models.CharField(choices=(( None, "test_none" ),))) 19 self.assertEqual(display_value, "test_none") 20 21 display_value = display_for_field(None, models.DateField()) 22 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 23 24 display_value = display_for_field(None, models.TimeField()) 25 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 26 27 display_value = display_for_field(None, models.NullBooleanField()) 28 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 29 30 display_value = display_for_field(None, models.DecimalField()) 31 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 32 33 display_value = display_for_field(None, models.FloatField()) 34 self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) 35