Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally change background color of cell in django admin list_display

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.

django version 1.10,

python 3.5


Model.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)

Results

    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)

enter image description here

like image 390
Stryker Avatar asked Oct 25 '25 09:10

Stryker


1 Answers

Try something like this:

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,
    )
like image 101
Eugene Prikazchikov Avatar answered Oct 27 '25 00:10

Eugene Prikazchikov