Fixed #26582 -- Added prettier admin display for list values.

This commit is contained in:
Dan Watson 2016-05-07 15:49:41 -04:00 committed by Tim Graham
parent 72ff70fba5
commit ad403ffa45
2 changed files with 12 additions and 3 deletions

View File

@ -402,7 +402,7 @@ def display_for_field(value, field, empty_value_display):
elif isinstance(field, models.FileField) and value:
return format_html('<a href="{}">{}</a>', value.url, value)
else:
return smart_text(value)
return display_for_value(value, empty_value_display)
def display_for_value(value, empty_value_display, boolean=False):
@ -418,6 +418,8 @@ def display_for_value(value, empty_value_display, boolean=False):
return formats.localize(value)
elif isinstance(value, six.integer_types + (decimal.Decimal, float)):
return formats.number_format(value)
elif isinstance(value, (list, tuple)):
return ', '.join(force_text(v) for v in value)
else:
return smart_text(value)

View File

@ -7,8 +7,8 @@ from django import forms
from django.conf import settings
from django.contrib.admin import helpers
from django.contrib.admin.utils import (
NestedObjects, display_for_field, flatten, flatten_fieldsets,
label_for_field, lookup_field, quote,
NestedObjects, display_for_field, display_for_value, flatten,
flatten_fieldsets, label_for_field, lookup_field, quote,
)
from django.db import DEFAULT_DB_ALIAS, models
from django.test import SimpleTestCase, TestCase, override_settings
@ -195,6 +195,13 @@ class UtilsTests(SimpleTestCase):
display_value = display_for_field(12345, models.IntegerField(), self.empty_value)
self.assertEqual(display_value, '12,345')
def test_list_display_for_value(self):
display_value = display_for_value([1, 2, 3], self.empty_value)
self.assertEqual(display_value, '1, 2, 3')
display_value = display_for_value([1, 2, 'buckle', 'my', 'shoe'], self.empty_value)
self.assertEqual(display_value, '1, 2, buckle, my, shoe')
def test_label_for_field(self):
"""
Tests for label_for_field