希望找到一种优雅的方法来有条不紊地更改list_filter中单元格的背景颜色。如果我没有条件,它适用于一种状态,但需要根据不同的状态更改背景颜色。
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
工作正常。但我相信有更好的方法可以做到这一点。
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)
答案 0 :(得分:2)
尝试这样的事情:
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,
)
答案 1 :(得分:0)
我已经尝试过了,但这只是在注释文字...对我来说不是解决方案。
我终于使用Jinja在模板中解决了这个问题,在每一行中询问我要填充的值。这可能不是最优雅的解决方案,但对我有帮助。
{% 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 %}
答案 2 :(得分:0)