首先创建自定义管理员 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; }
我开发这个解决方案是因为我需要根据实例值对几乎每个单元格进行着色。检查 Django==1.6.2 和 django-grappelli==2.5.2