summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-12-28 15:31:45 -0500
committerTim Graham <timograham@gmail.com>2016-02-02 10:22:59 -0500
commit37f7ef41fbaebd5f545b99a11ab6036d92f9232b (patch)
tree0e3cad21f2e7827278cf54a1cd959abfcae24061 /django
parent1e9150443e5696d764ed81c97b53ef0365a5d854 (diff)
Fixed #24316 -- Made ModelAdmin.list_display callables use an appropriate CSS class name.
Thanks Berker Peksag for the review.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/admin/templatetags/admin_list.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
index a1fd3b5657..c8ebccfd25 100644
--- a/django/contrib/admin/templatetags/admin_list.py
+++ b/django/contrib/admin/templatetags/admin_list.py
@@ -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: