Ticket #12550: django_nullvalue_adminlist_display.patch

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

Patch for other fields too and includes regression test.

  • 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   
     285    if value is None:
     286        return EMPTY_CHANGELIST_VALUE
     287       
    284288    if isinstance(field, models.DateField) or isinstance(field, models.TimeField):
    285289        return formats.localize(value)
    286290    elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
  • 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        display_value = display_for_field(None, models.DateField())
     14        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
     15       
     16        display_value = display_for_field(None, models.TimeField())
     17        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
     18       
     19        display_value = display_for_field(None, models.NullBooleanField())
     20        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
     21       
     22        display_value = display_for_field(None, models.DecimalField())
     23        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
     24       
     25        display_value = display_for_field(None, models.FloatField())
     26        self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
     27
Back to Top