list_display - boolean icons for methods

Jason McClellan picture Jason McClellan · Nov 22, 2011 · Viewed 24.9k times · Source

When defining the list_display array for a ModelAdmin class, if a BooleanField or NullBooleanField is given the UI will use nice looking icons instead of True/False text in the column. If a method that returns a boolean is given, however, it simply prints out True/False.

Is there a way to make it use the pretty icons for a boolean method?

Answer

Daniel Roseman picture Daniel Roseman · Nov 22, 2011

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