list_display - boolean icons for methods list_display - boolean icons for methods django django

list_display - boolean icons for methods


This is documented, although it's a bit hard to find - go a couple of screens down from here, and you'll find this:

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.

and the example given is:

def born_in_fifties(self):    return self.birthday.strftime('%Y')[:3] == '195'born_in_fifties.boolean = True


Thanks to @daniel-roseman (rtfm)
Since Django 3.2 there is a decorator @admin.display(boolean=True):

If the string (in list_display) given is a method of the model,ModelAdmin or a callable that returns True, False, or None, Djangowill display a pretty “yes”, “no”, or “unknown” icon if you wrap themethod with the display() decorator passing the boolean argument withthe value set to True:

class Person(models.Model):    birthday = models.DateField()    @admin.display(boolean=True)    def born_in_fifties(self):        return 1950 <= self.birthday.year < 1960