In the Django admin if the field is a BooleanField or NullBooleanField, Django will display a pretty "on" or "off" icon instead of True or False.
Now, I don't really have a BooleanField in my model by I do have a property fior which I'd like to display the icons but when I try doing so, Django screams that 'SomeAdmin.list_filter[0]' refers to 'is_activated' which does not refer to a Field.
Is it possible to display those nice little icons for this field without hacking Django too much.
Thanks
You don't want to use list_filter
. The property you're looking for is list_display
. The documentation offers an example of how you can create a column that behaves like a boolean in the display. In short, you do something like this:
Create a method in the class:
def is_activated(self)
if self.bar == 'something':
return True
return False
add the .boolean
method attribute directly below the is_activated
method:
is_activated.boolean = True
Add the method as a field in list_display
:
class MyAdmin(ModelAdmin): list_display = ['name', 'is_activated']
You'll notice the column name is probably now "Is Activated" or something like that. If you want the column heading to change, you use the short_description
method attribute:
is_activated.short_description = "Activated"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With