[Fixed]-Django admin: change cell value color depending on content

1👍

estado_material can be changed to something like the following using format_html method from django.utils.html:

from django.utils.html import format_html
...
    def estado_material(self):
        if Prestamos.objects.filter(material__num_com=self.num_com).exists():
            result = "En préstamo"
        else:
            result = "Disponible"
        return format_html('<span style="color: {};">{}</span>',
                       'green' if  result == 'Disponible' else 'red',
                       result)

Before Django 1.9 allow_tags attribute could be added to the method to prevent auto-escaping – it is now deprecated and format_html is sufficient.

Leave a comment