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

Str*_*ker 2 django list django-admin display

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

Django 1.10 版,

蟒蛇 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)
Run Code Online (Sandbox Code Playgroud)

管理文件

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)
Run Code Online (Sandbox Code Playgroud)

结果

    else obj.status =='Needs Info':
           ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

只有一个条件

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

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Eug*_*kov 5

尝试这样的事情:

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,
    )
Run Code Online (Sandbox Code Playgroud)