Clayton Clayton - 1 month ago 11x
Python Question

Show Django Model @property as a bool in model admin

I have a model with a

, It returns boolean, I want to show it as icon in django model admin.

class Foo(models.Model):
bar = models.TextField("Title", null=True, blank=True)

def is_new_bar(self):
return bar == 'NEW'

class FooAdmin(admin.ModelAdmin):
list_display = ('bar', 'is_new_bar') # is_new_bar is shown as True/False text, I want this as bool icon of django.

Boolean Icons


You can add method to your modeladmin that will return property value and set that it will return boolean:

class FooAdmin(admin.ModelAdmin):
     list_display = ('bar', 'get_is_new_bar') 

     def get_is_new_bar(self, obj):
        return self.is_new_bar
     get_is_new_bar.boolean = True