2011-11-22 36 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