2011-11-22 4 views
86

ModelAdminクラスのlist_display配列を定義するとき、BooleanFieldまたはNullBooleanFieldが指定されている場合、そのUIはその列にTrue/Falseテキストの代わりにnice lookingアイコンを使用します。しかし、ブール値を返すメソッドが与えられた場合、それは単純に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: 'プロパティ'オブジェクトには 'boolean'属性がありません" –

+3

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

+0

これは非常に役に立ちます – minhajul