In the previous chapter, How to enable filtering on calculated fields? you added a boolean field.:
def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75
Which looks like this
![_images/filter_calculated_fixed.png](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/_images/filter_calculated_fixed.png)
The
is_very_benevolent
field shows the string True and False, unlike the builtin BooleanFields which show an on and off indicator. To fix this, you add a boolean
attribute on your method. You final modeladmin looks like this:@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
And your admin looks like this
![_images/boolean_field_fixed.png](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/_images/boolean_field_fixed.png)
No comments:
Post a Comment