The Python Oracle

My own method used in list_display and value as boolean icon

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 My Own Method Used In List_display And Value As Boolean Icon
00:17 Accepted Answer Score 12
00:45 Thank you

--

Full question
https://stackoverflow.com/questions/1139...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangoadmin

#avk47



ACCEPTED ANSWER

Score 12


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.