首先創造一個加載自定義模板標籤定製管理change_list模板:
{% load i18n grp_tags admin_urls admin_static admin_list my_admin_list %}
複製現有admin_list.py和變化:
def items_for_result(cl, result, form):
if f is None:
...
css_class = getattr(attr, 'css_class', False)
if css_class:
row_class = mark_safe(' class="%s"' % css_class(result))
In admin.py have something like:
class Media:
css = {
'all': ('/static/custom_admin.css',)
}
...
def job_driver_car(self, obj):
return '{0} <strong>{1}</strong><br>{2}'.\
format(obj.driver_type,
obj.driver_grade,
obj.car_type)
job_driver_car.short_description = 'Driver/Car'
job_driver_car.css_class = lambda obj: 'background lightyellow' \
if obj.driver_grade is not None and 'NEW' in obj.driver_grade else ''
job_driver_car.allow_tags = True
和CSS本身:
table tbody tr.grp-row-odd td.background, table tbody tr.grp-row-odd th.background,
table tbody tr.grp-row-even td.background, table tbody tr.grp-row-even th.background,
table tbody tr.grp-alt td.background, table tbody tr.grp-alt th.background {
background: transparent;
}
.lightyellow { background: #ffd090 !important; }
我開發這個解決方案,因爲我需要根據實例對幾乎每個單元格進行着色ce值。檢查了Django == 1.6.2和django-grappelli == 2.5.2