如果表达式为 True,则更改 Django 管理界面中字段的字体/颜色 [英] Change font/color for a field in django admin interface if expression is True

查看:23
本文介绍了如果表达式为 True,则更改 Django 管理界面中字段的字体/颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django管理界面的更改列表视图中,如果某些字段/行实现了表达式,是否可以将它们标记为红色?

In change list view in django admin interface, is it possible to mark some fields/rows red in if they achieve a expression?

例如,如果有一个模型 Group 具有 memberscapacity,我如何可视化它们full 还是 拥挤?

For example, if there is a model Group with members and capacity, how can I visualize when they are full or crowded?

推荐答案

这是一个老问题,但我会从 Django 1.10 的文档中添加一个示例,因为 allow_tagsDjango 1.9 起,已接受的答案中使用的属性已被弃用,建议使用 format_html 代替:

This is an old question but I'll add an example from docs for Django 1.10 because allow_tags attribute used in the accepted answer is deprecated since Django 1.9 and it is recommended to use format_html instead:

from django.db import models
from django.contrib import admin
from django.utils.html import format_html

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)

    def colored_name(self):
        return format_html(
            '<span style="color: #{};">{} {}</span>',
            self.color_code,
            self.first_name,
            self.last_name,
        )

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

这篇关于如果表达式为 True,则更改 Django 管理界面中字段的字体/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆