Display check mark and cross icons for property in Django admin console

In the Django admin, if the field is a BooleanField or NullBooleanField, Django will display a nice "on" or "off" icon instead of True or False.

Now I donโ€™t actually have a BooleanField in my model, I have a fior property that I would like to display icons, but when I try to do this, Django screams that 'SomeAdmin.list_filter[0]' refers to 'is_activated' which does not refer to a Field.

Is it possible to display these cute little icons for this field without hacking Django too much.

thanks

+6
source share
1 answer

You do not want to use list_filter . The property you are looking for is list_display . The documentation provides an example of how you can create a column that behaves like a boolean on a display. In short, you are doing something like this:

  • Create a method in the class:

     def is_activated(self) if self.bar == 'something': return True return False 
  • add the attribute of the .boolean method directly below the is_activated method:

     is_activated.boolean = True 
  • Add the method as a field to list_display :

    class MyAdmin (ModelAdmin): list_display = ['name', 'is_activated']

  • You will notice that the column name is probably now โ€œactivatedโ€ or something like that. If you want the column heading to change, you use the short_description method short_description :

     is_activated.short_description = "Activated" 
+16
source

Source: https://habr.com/ru/post/897626/


All Articles