Django admin listview column names
In Django's admin app, you can easily change what columns appear for a given model's list view. For example, if I have a "Manager" model and I want to display the number of underlings in their line item in admin, that's easy, assuming the model has a method to count up underlings (we'll say it does, Manager.underlings_count()
):
class ManagerAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'underlings_count')
But what if I don't want the column header to be called 'Underlings count'? The name is very descriptive and good in code, but in admin 'count' is just wasted space - if you're looking at a list of numbers, it's obviously a count.
The workaround to change it is super simple:
class ManagerAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'underlings')
def underlings(self, obj):
# in this context, obj is the Manager instance for this line item
return obj.underlings_count()
Boom. Retitled column just by defining a proxy function.
Written by Alex Whittemore
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#