Ticket #12550: django_nullvalue_adminlist_display.2.patch

File django_nullvalue_adminlist_display.2.patch, 3.4 KB (added by Jacques Beaurain <jacques.beaurain@…>, 15 years ago)

Better patch for flatchoices and one more type

  • django/contrib/admin/util.py

     
    281281def display_for_field(value, field):
    282282    from django.contrib.admin.templatetags.admin_list import _boolean_icon
    283283    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):
    285290        return formats.localize(value)
    286291    elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
    287292        return _boolean_icon(value)
     
    289294        return formats.number_format(value, field.decimal_places)
    290295    elif isinstance(field, models.FloatField):
    291296        return formats.number_format(value)
    292     elif field.flatchoices:
    293         return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
    294297    else:
    295298        return smart_unicode(value)
  • tests/regressiontests/admin_util/models.py

     
     1from django.db import models
     2
     3       
     4 No newline at end of file
  • tests/regressiontests/admin_util/tests.py

     
     1import unittest
     2from django.contrib import admin
     3from django.db import models
     4from django.contrib.admin.util import display_for_field
     5from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
     6
     7class 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       
Back to Top