gpt4 book ai didi

django - 有条件地更改 django admin list_display 中单元格的背景颜色

转载 作者:行者123 更新时间:2023-12-04 16:08:50 25 4
gpt4 key购买 nike

希望找到一种优雅的方法来有条件地更改 list_filter 中单元格的背景颜色。如果我没有条件,它适用于一种状态,但需要根据不同的状态更改背景颜色。

django 1.10 版,

python 3.5


模型.py
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

只有一个条件

工作正常。但我确信有更好的方法来做到这一点。

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)

enter image description here

最佳答案

试试这样的:

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 - 有条件地更改 django admin list_display 中单元格的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40290898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com