Add column-<field_name> classes to the admin list

This simplifies CSS styling to set column widths.
This commit is contained in:
Diederik van der Boor 2013-02-24 17:31:26 +01:00
parent 8503120c10
commit b7ba4fc408
3 changed files with 13 additions and 3 deletions

View File

@ -12,10 +12,9 @@ from django.db import models
from django.utils import formats from django.utils import formats
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.encoding import smart_text, force_text from django.utils.encoding import force_text
from django.template import Library from django.template import Library
from django.template.loader import get_template from django.template.loader import get_template
from django.template.context import Context from django.template.context import Context
@ -112,12 +111,13 @@ def result_headers(cl):
# Not sortable # Not sortable
yield { yield {
"text": text, "text": text,
"class_attrib": format_html(' class="column-{0}"', field_name),
"sortable": False, "sortable": False,
} }
continue continue
# OK, it is sortable if we got this far # OK, it is sortable if we got this far
th_classes = ['sortable'] th_classes = ['sortable', 'column-{0}'.format(field_name)]
order_type = '' order_type = ''
new_order_type = 'asc' new_order_type = 'asc'
sort_priority = 0 sort_priority = 0

View File

@ -543,6 +543,13 @@ subclass::
The above will tell Django to order by the ``first_name`` field when The above will tell Django to order by the ``first_name`` field when
trying to sort by ``colored_first_name`` in the admin. trying to sort by ``colored_first_name`` in the admin.
* .. versionadded:: 1.6
The field names in ``list_display`` will also appear as CSS classes in
the HTML output, in the form of ``column-<field_name>`` on each ``<th>``
element. This can be used to set column widths in a CSS file for example.
.. attribute:: ModelAdmin.list_display_links .. attribute:: ModelAdmin.list_display_links
Set ``list_display_links`` to control which fields in ``list_display`` Set ``list_display_links`` to control which fields in ``list_display``

View File

@ -101,6 +101,9 @@ Minor features
context through to feed templates using a new `Feed.get_context_data()` context through to feed templates using a new `Feed.get_context_data()`
callback. callback.
* The admin list columns have a ``column-<field_name>`` class in the HTML
so the columns header can be styled with CSS, e.g. to set a column width.
Backwards incompatible changes in 1.6 Backwards incompatible changes in 1.6
===================================== =====================================