Clayton Clayton - 3 months ago 27
Python Question

Show Django Model @property as a bool in model admin

I have a model with a

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

models.py



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

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


admin.py



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

Answer

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