2👍
✅
The list_display
option allows you to specify functions on the ModelAdmin
itself to display as columns. You don’t even need to define that function on your Question
model, if you’re only going to need it for your ModelAdmin
.
From the docs of list_display
:
class PersonAdmin(admin.ModelAdmin):
list_display = ('upper_case_name',)
def upper_case_name(self, obj):
return ("%s %s" % (obj.first_name, obj.last_name)).upper()
upper_case_name.short_description = 'Name'
A function specified in your list_display
attribute will be called with a single parameter: the object that is being displayed. You can obtain the count of its related instances using RelatedManager.count
on your reverse relation:
return obj.choice_set.count()
Source:stackexchange.com