The Python Oracle

My own method used in list_display and value as boolean icon

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Cool Puzzler LoFi

--

Chapters
00:00 Question
00:28 Accepted answer (Score 12)
01:07 Thank you

--

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

Accepted answer links:
[documentation]: https://docs.djangoproject.com/en/dev/re...

--

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.