django后台对某些字段设置颜色

时间:2024-09-08 23:05:02

在model.py的class函数中添加如下代码

#对签收状态设置颜色
def color_state(self):
if self.assign_notice_state == '':
assign_state_name = '待签收'
color_code = 'red'
else:
color_code = 'green'
assign_state_name = '已签收'
return format_html(
'<span style="color:{};">{}</span>',
color_code,
assign_state_name,
)
color_state.short_description = '签收状态'

adminx.py文件

list_display = ['assign_notice_title',....,'color_state','qianshou']

效果

django后台对某些字段设置颜色