Django admin listview Customize Column Name
Solution 1
Use:
class AuthorAdmin(admin.ModelAdmin):
…
def my_function(self, obj) :
"""My Custom Title"""
…
my_function.short_description = 'This is the Column Name'
It's buried in the admin docs. short_description
, specifically, is barely mentioned under the discussion of list_display
(more by example than actually called out). The other items like this are similiarly buried in the admin docs, but here's a summary:
-
short_description
: the column title to use (string) -
allow_tags
: what the name says... let's you use HTML (True
orFalse
) -
admin_order_field
: a field on the model to order this column by (string, field name) -
boolean
: indicates the return value is boolean and signals the admin to use the nice graphic green check/red X (True
orFalse
)
Solution 2
Starting from Django 3.2 you can use the display
decorator. It has the attribute description
for changing the name of the column:
class AuthorAdmin(admin.ModelAdmin):
list_display = ['profile_photo', 'first_name', 'last_name', 'title']
@admin.display(description='Profile Photo')
def profile_photo(self, obj) :
return '<img src="%s" title="%s" />' % (resize_image(obj.photo, '100x100'), obj.title)
For more info about the display
decorator see this page
Related videos on Youtube
Francis Yaconiello
I do a bunch of: Python Django Javascript Vue Node PHP WordPress Drupal 8 Search Solr Elasticsearch
Updated on July 09, 2022Comments
-
Francis Yaconiello almost 2 years
Ok so I have a custom django admin built from a Author Model:
class AuthorAdmin(admin.ModelAdmin): """ Author Admin """ form = AuthorForm list_display = ['profile_photo', 'first_name', 'last_name', 'title'] search_fields = ['first_name', 'last_name', 'title', 'credential'] prepopulated_fields = {'slug': ('first_name', 'last_name', 'title')} def profile_photo(self, obj) : return '<img src="%s" title="%s" />' % (resize_image(obj.photo, '100x100'), obj.title) profile_photo.allow_tags = True
But in the django admin listview the column title for the custom column does not have proper capitalization.
Does anyone know how to override the column headers that are built from custom function's names?
I've tried:
def my_function(self, obj) : """My Custom Title""" ...
and
def my_function(self, obj) : class Meta: verbose_name = _(u"My Custom Title")
-
artu-hnrq about 3 yearsFrom what module are you importing
resize_image
function? -
Francis Yaconiello about 3 yearsoh man this is like 10 years old, probably sorl thumbnail? @artu-hnrq
-
-
Francis Yaconiello about 12 yearsworks great, is there some page in the docs where I could read more about this and other features like it?
-
cxxl almost 3 yearsUnfortunately, this destroys the possibility to sort by this column
-
CSSer almost 3 years@cxxl I found this
-
saadat ali over 2 yearsCompletely irrelevant answer, renaming a column in admin panel has nothing to do with verbose_name in models
-
sebieire over 2 yearsHave you actually tried this before down voting the answer? This offers an alternative solution to the original question. Please check first and revisit.
-
saadat ali over 2 yearsYes bro, I have tried it yesterday & is not working this way.
-
sebieire over 2 yearsThat's really odd. I've just double checked it and it works fine. Are you sure you did the migrations after changing it? In my models.py I have
field_name = models.CharField(...., verbose_name='Whatever')
and in my admin.py I have the fields registered as needed withlist_display=['field_name',...]
. And that does exactly what I explained above. I am on Django 3.2. -
saadat ali over 2 yearsI am using django 3.2 myself on python3.6 and yes i applied migrations but it didnt show the title as specified in verbose_name.
def project_logo_ftn(self, obj): return format_html('<img src="{}" width="auto" height="200px" />'.format(obj.project_logo.url)) project_logo_ftn.short_description = 'Project Logo'
I used this method to display the image and set the name to project Logo in admin -
saadat ali over 2 yearsmay be your method work for non image fields only.
-
sebieire over 2 years@saadatali this is what I am starting to think as well. Could be that this works only on character like fields maybe. Image (and other type) fields might not have the property 'verbose_name' available. That would explain it.