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

标签 django list django-admin display

希望找到一种优雅的方法来有条件地更改 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/

相关文章:

database - 在django模型中,如何让所有的表名都没有app标签?

python - 在 Django 测试中,我应该如何保存数据库对象然后从数据库中检索它?

java - setVisibility 列表<item>

django - 如何在重写 save_model 方法时防止出现 "Changed successfully"消息

django - 在另一个下拉列表上进行选择时,如何在Django管理员中过滤下拉列表

django - 上传到 Django 表单时验证文件内容

django - 如何覆盖表单的默认值?

php - 为什么 file_get_contents 适用于 google.com 但不适用于我的网站?

python - 将矩阵(列表列表)转换为python中的方阵

c# - list <T> : Clear; AddRange; Add; Delete; ect. 。导致 System.IndexOutOfRangeException