Back to writing

Article

Showing On/Off Icons for Calculated Boolean Fields

Django admin can display its standard yes/no icons for a calculated value, not only for model BooleanFields. Add the method to list_display, return a Boolean value, and mark the display method as Boolean so the admin renders the appropriate icon.

@admin.register(Hero) class HeroAdmin(admin.ModelAdmin):    list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent")    list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)    def is_very_benevolent(self, obj):        return obj.benevolence_factor > 75    is_very_benevolent.boolean = True    is_very_benevolent.short_description = 'benevolent'