1
0
mirror of https://github.com/django/django.git synced 2025-10-26 23:26:08 +00:00

Fixed #24316 -- Made ModelAdmin.list_display callables use an appropriate CSS class name.

Thanks Berker Peksag for the review.
This commit is contained in:
Tim Graham
2015-12-28 15:31:45 -05:00
parent 1e9150443e
commit 37f7ef41fb
3 changed files with 25 additions and 3 deletions

View File

@@ -104,6 +104,7 @@ def result_headers(cl):
return_attr=True
)
if attr:
field_name = _coerce_field_name(field_name, i)
# Potentially not sortable
# if the field is the action checkbox: no sorting and special class
@@ -183,6 +184,18 @@ def _boolean_icon(field_val):
return format_html('<img src="{}" alt="{}" />', icon_url, field_val)
def _coerce_field_name(field_name, field_index):
"""
Coerce a field_name (which may be a callable) to a string.
"""
if callable(field_name):
if field_name.__name__ == '<lambda>':
return 'lambda' + str(field_index)
else:
return field_name.__name__
return field_name
def items_for_result(cl, result, form):
"""
Generates the actual list of data.
@@ -197,9 +210,9 @@ def items_for_result(cl, result, form):
first = True
pk = cl.lookup_opts.pk.attname
for field_name in cl.list_display:
for field_index, field_name in enumerate(cl.list_display):
empty_value_display = cl.model_admin.get_empty_value_display()
row_classes = ['field-%s' % field_name]
row_classes = ['field-%s' % _coerce_field_name(field_name, field_index)]
try:
f, attr, value = lookup_field(field_name, result, cl.model_admin)
except ObjectDoesNotExist: