有条件地更改Django管理员list_display中单元格的背景颜色 [英] Conditionally change background color of cell in django admin list_display
本文介绍了有条件地更改Django管理员list_display中单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图找到一种有条件的方式来有条件地更改list_filter中单元格的背景颜色.如果我没有条件,则它可以在一种状态下正常工作,但需要根据不同的状态更改背景色.
Looking to find an elegant way to conditionally change the background color of a cell in list_filter. If I don't have a conditional, it works fine for one status but need to change the color of background based on different status.
class PlayBook(TimeStampModel):
minor = 'MINOR'
normal = 'NORMAL'
important = 'IMPORTANT'
critical = 'CRITICAL'
SEVERITY = (
(minor, 'Minor'),
(normal, 'Normal'),
(important, 'Important'),
(critical, 'Critical'),
)
low = 'LOW'
high = 'HIGH'
PRIORITY = (
(low, 'Low'),
(normal, 'Normal'),
(high, 'High'),
)
new = 'New'
in_progress = 'In_Progress'
needs_info = 'Needs Info'
postponed = 'Postponed'
closed = 'Closed'
STATUS= (
(new, 'New'),
(in_progress, 'In Progress'),
(needs_info, 'Needs Info'),
(postponed, 'Postponed'),
(closed, 'Closed'),
)
subject = models.CharField(max_length=200, unique=True)
description = models.TextField(blank=True, help_text="Business purpose of the application")
manager = models.ForeignKey(User, on_delete=models.CASCADE)
severity = models.CharField(max_length = 100, choices=SEVERITY, default=normal)
priority = models.CharField(max_length = 100, choices=PRIORITY, default=normal)
status = models.CharField(max_length = 100, choices=STATUS, default=new)
def __str__(self):
return "{}".format(self.subject)
class Meta:
ordering = ('severity',)
@property
def short_description(self):
return truncatechars(self.description, 35)
Admin.py
from django.utils.html import format_html
class PlayBookAdmin(admin.ModelAdmin):
list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']
def status_colored(self, obj):
color = 'yellow'
if obj.status == 'Closed':
color = 'green'
return format_html(
'<b style="background:{};">{}</b>',
color,
obj.status
)
elif obj.status =='In Progress':
color = 'yellow'
return format_html(
'<b style="background:{};">{}</b>',
color,
obj.status
)
else obj.status =='Needs Info':
color = 'orange'
return format_html(
'<b style="background:{};">{}</b>',
color,
obj.status
)
status_colored.admin_order_field = 'closed'
admin.site.register(PlayBook, PlayBookAdmin)
结果
else obj.status =='Needs Info':
^
SyntaxError: invalid syntax
只有一个条件
工作正常.但是我相信有更好的方法可以做到这一点.
With only one condition
Works fine. But I am sure there is a better way to do this.
from django.utils.html import format_html
class PlayBookAdmin(admin.ModelAdmin):
list_display =['severity','priority', 'subject', 'status_colored','created','updated', 'short_description']
def status_colored(self, obj):
color = 'yellow'
if obj.status == 'Closed':
color = 'green'
return format_html(
'<b style="background:{};">{}</b>',
color,
obj.status
)
status_colored.admin_order_field = 'closed'
admin.site.register(PlayBook, PlayBookAdmin)
推荐答案
尝试如下操作:
def status_colored(self, obj):
colors = {
'Closed': 'green',
'In Progress': 'yellow',
'Needs Info': 'orange',
}
return format_html(
'<b style="background:{};">{}</b>',
colors[obj.status],
obj.status,
)
这篇关于有条件地更改Django管理员list_display中单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文