Reputation: 6120
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)
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
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)
Upvotes: 4
Views: 6305
Reputation: 31
I made test with text color, not background color:
def visited(self, obj):
text = 'Not Visited'
color = 'red'
if obj.status== True:
text = 'Visited'
color = 'green'
return format_html('<b style="color:{};">{}</b>',color,text,obj.status)
Upvotes: 3
Reputation: 316
I've tried this, but this just only remark the text... Were not a solutions for me.
I've finally resolve this in the template using Jinja, asking in every row for the value I want to fill. This could be not the most elegant solution, but helps me.
{% for row in cl.result_list %}
<tr class="{% cycle 'row1' 'row2' %}" {% if row.etapa == 50 %} style='background-color:#ffcccc' {% endif %} in>
<td> {{ row.fecha }} </td>
<td> {{ row.etapa }} </td>
<td> {{ row.presion }} </td>
<td> {{ row.tempin }} </td>
<td>
</tr>{% endfor %}
Upvotes: 0
Reputation: 1904
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,
)
Upvotes: 7