Ensure that NullBooleanField displays the appropriate icon for null values in admin changelists. Refs #13071.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12746 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
James Bennett 2010-03-10 08:37:17 +00:00
parent baa4d3b710
commit a0c77ada99
4 changed files with 20 additions and 3 deletions

View File

@ -337,12 +337,14 @@ def display_for_field(value, field):
if field.flatchoices: if field.flatchoices:
return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
# NullBooleanField needs special-case null-handling, so it comes
# before the general null test.
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
return _boolean_icon(value)
elif value is None: elif value is None:
return EMPTY_CHANGELIST_VALUE return EMPTY_CHANGELIST_VALUE
elif isinstance(field, models.DateField) or isinstance(field, models.TimeField): elif isinstance(field, models.DateField) or isinstance(field, models.TimeField):
return formats.localize(value) return formats.localize(value)
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
return _boolean_icon(value)
elif isinstance(field, models.DecimalField): elif isinstance(field, models.DecimalField):
return formats.number_format(value, field.decimal_places) return formats.number_format(value, field.decimal_places)
elif isinstance(field, models.FloatField): elif isinstance(field, models.FloatField):

View File

@ -1,6 +1,7 @@
from datetime import datetime from datetime import datetime
import unittest import unittest
from django.conf import settings
from django.db import models from django.db import models
from django.utils.formats import localize from django.utils.formats import localize
from django.test import TestCase from django.test import TestCase
@ -131,8 +132,11 @@ class UtilTests(unittest.TestCase):
display_value = display_for_field(None, models.TimeField()) display_value = display_for_field(None, models.TimeField())
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
# Regression test for #13071: NullBooleanField has special
# handling.
display_value = display_for_field(None, models.NullBooleanField()) display_value = display_for_field(None, models.NullBooleanField())
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) expected = u'<img src="%simg/admin/icon-unknown.gif" alt="None" />' % settings.ADMIN_MEDIA_PREFIX
self.assertEqual(display_value, expected)
display_value = display_for_field(None, models.DecimalField()) display_value = display_for_field(None, models.DecimalField())
self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE) self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)

View File

@ -464,11 +464,13 @@ class Post(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
content = models.TextField() content = models.TextField()
posted = models.DateField(default=datetime.date.today) posted = models.DateField(default=datetime.date.today)
public = models.NullBooleanField()
def awesomeness_level(self): def awesomeness_level(self):
return "Very awesome." return "Very awesome."
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'public']
readonly_fields = ('posted', 'awesomeness_level', 'coolness', lambda obj: "foo") readonly_fields = ('posted', 'awesomeness_level', 'coolness', lambda obj: "foo")
inlines = [ inlines = [

View File

@ -253,6 +253,15 @@ class AdminViewBasicTest(TestCase):
"Changelist filter isn't showing options contained inside a model field 'choices' option named group." "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
) )
def testChangeListNullBooleanDisplay(self):
Post.objects.create(public=None)
# This hard-codes the URl because it'll fail if it runs
# against the 'admin2' custom admin (which doesn't have the
# Post model).
response = self.client.get("/test_admin/admin/admin_views/post/")
self.failUnless('icon-unknown.gif' in response.content)
print "Passed"
class SaveAsTests(TestCase): class SaveAsTests(TestCase):
fixtures = ['admin-views-users.xml','admin-views-person.xml'] fixtures = ['admin-views-users.xml','admin-views-person.xml']