2011-11-22 37 views
86

ModelAdmin类定义list_display数组时,如果给出BooleanFieldNullBooleanField,UI将使用精美的图标而不是列中的True/False文本。但是,如果给出一个返回布尔值的方法,它只是输出True/False。list_display - 方法的布尔图标

有没有办法让它使用布尔方法的漂亮图标?

回答

163

这证明,虽然这是一个有点难找 - 从here走一对夫妇的屏幕下,你会发现这一点:

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 is True .

和给出的例子是:

def born_in_fifties(self): 
    return self.birthday.strftime('%Y')[:3] == '195' 
born_in_fifties.boolean = True 
+9

任何想法如何做到这一点当你使用@property装饰器?:“AttributeError:'属性'对象没有属性'布尔值' –

+3

@Kye http://stackoverflow.com/questions/12842095/how-to-display -a-boolean-property-in-the-django-admin –

+0

这非常有帮助 – minhajul