django models 字体颜色

时光如瑾雨微凉 2019-12-26

def colored_name(self):
    if self.状态 == ‘Y‘:
        color_code = ‘green‘
    else:
        color_code = ‘red‘
    return format_html(
        ‘<span style="color:{};">{}</span>‘,
        color_code,
        self.状态
    )

但如果你给这个方法添加一个boolean的属性并赋值为True,它将显示为on/off的图标,如下图:

from django.db import models
from django.contrib import admin
class Person(models.Model):
    first_name = models.CharField(max_length=50)
    birthday = models.DateField()

    def born_in_fifties(self):
        return self.birthday.strftime(‘%Y‘)[:3] == ‘195‘
    # 关键在这里
    born_in_fifties.boolean = True

class PersonAdmin(admin.ModelAdmin):
    # 官方文档这里有错,将‘name‘改为‘first_name‘ 
    list_display = (‘first_name‘, ‘born_in_fifties‘)

django models 字体颜色

 

但是,我们看到标题并不是我们想要的,那么如何设置标题呢?
 
添加一行代码:colored_status.short_description = u"状态"
django models 字体颜色

 

相关推荐

inspuryhq / 0评论 2020-07-28