list_display-方法的布尔图标 [英] list_display - boolean icons for methods
问题描述
在为 ModelAdmin
类定义list_display数组时,如果 BooleanField
或 NullBooleanField
表示UI将使用美观的图标代替该列中的True / False文本。但是,如果给出了一个返回布尔值的方法,它只会打印出True / False。
When defining the list_display array for a ModelAdmin
class, if a BooleanField
or NullBooleanField
is given the UI will use nice looking icons instead of True/False text in the column. If a method that returns a boolean is given, however, it simply prints out True/False.
有没有办法使它使用漂亮的图标作为布尔方法?
Is there a way to make it use the pretty icons for a boolean method?
推荐答案
已记录此文档,尽管很难找到它-从在这里,您会发现:
This is documented, although it's a bit hard to find - go a couple of screens down from here, and you'll find this:
如果给定的字符串是模型的方法,则如果您为该方法赋予
boolean ,则ModelAdmin或返回True或False的callable Django将显示一个漂亮的 on或 off图标。 code>属性的值为
True
。
If the string given is a method of the model, ModelAdmin or a callable that returns True or False Django will display a pretty "on" or "off" icon if you give the method a
boolean
attribute whose value isTrue
.
给出的是:
def born_in_fifties(self):
return self.birthday.strftime('%Y')[:3] == '195'
born_in_fifties.boolean = True
这篇关于list_display-方法的布尔图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!