Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My own method used in list_display and value as boolean icon

I wrote my own method used in list_display (admin class), like this:

class MyClassAdmin(admin.ModelAdmin):

    list_display = ('my_own_method')

    def my_own_method(self, obj):
        if [condition]:       
            return True
        else: 
            return False

but this value is displayed on list as text (True or False), not as default django boolean icons like this: enter image description here

What should I do to change this?

like image 819
David Silva Avatar asked Jul 09 '12 10:07

David Silva


1 Answers

Change your code to the following:

class MyClassAdmin(admin.ModelAdmin):

    list_display = ('my_own_method')

    def my_own_method(self, obj):
        if [condition]:       
            return True
        else: 
            return False
    my_own_method.boolean = True

which can be found in the documentation on list_display:

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.

like image 56
Simeon Visser Avatar answered Nov 15 '22 14:11

Simeon Visser