Display tick and cross icons for a property in the Django administration console
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: Techno Intrigue Looping
--
Chapters
00:00 Question
00:48 Accepted answer (Score 34)
02:01 Answer 2 (Score 2)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/7474...
Accepted answer links:
[list_display]: https://docs.djangoproject.com/en/1.0/re...
Answer 2 links:
[@admin.display]: https://docs.djangoproject.com/en/3.2/re...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangoadmin
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Intrigue Looping
--
Chapters
00:00 Question
00:48 Accepted answer (Score 34)
02:01 Answer 2 (Score 2)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/7474...
Accepted answer links:
[list_display]: https://docs.djangoproject.com/en/1.0/re...
Answer 2 links:
[@admin.display]: https://docs.djangoproject.com/en/3.2/re...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangoadmin
#avk47
ACCEPTED ANSWER
Score 38
You don't want to use list_filter. The property you're looking for is list_display. The documentation offers an example of how you can create a column that behaves like a boolean in the display. In short, you do something like this:
Create a method in the class:
def is_activated(self) if self.bar == 'something': return True return Falseadd the
.booleanmethod attribute directly below theis_activatedmethod:is_activated.boolean = TrueAdd the method as a field in
list_display:class MyAdmin(ModelAdmin): list_display = ['name', 'is_activated']
You'll notice the column name is probably now "Is Activated" or something like that. If you want the column heading to change, you use the
short_descriptionmethod attribute:is_activated.short_description = "Activated"
ANSWER 2
Score 9
The correct way to do this now in Django 3.0+ is with @admin.display.
@admin.display( boolean=True, ordering='-publish_date', description='Is Published?', ) def is_published(self, obj): return obj.publish_date is not None