Django admin listview Customize Column Name

29,747

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 or False)
  • 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 or False)

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

Share:
29,747

Related videos on Youtube

Francis Yaconiello
Author by

Francis Yaconiello

I do a bunch of: Python Django Javascript Vue Node PHP WordPress Drupal 8 Search Solr Elasticsearch

Updated on July 09, 2022

Comments

  • Francis Yaconiello
    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. capitalization matters dammit!

    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
      artu-hnrq about 3 years
      From what module are you importing resize_image function?
    • Francis Yaconiello
      Francis Yaconiello about 3 years
      oh man this is like 10 years old, probably sorl thumbnail? @artu-hnrq
  • Francis Yaconiello
    Francis Yaconiello about 12 years
    works great, is there some page in the docs where I could read more about this and other features like it?
  • cxxl
    cxxl almost 3 years
    Unfortunately, this destroys the possibility to sort by this column
  • CSSer
    CSSer almost 3 years
    @cxxl I found this
  • saadat ali
    saadat ali over 2 years
    Completely irrelevant answer, renaming a column in admin panel has nothing to do with verbose_name in models
  • sebieire
    sebieire over 2 years
    Have 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
    saadat ali over 2 years
    Yes bro, I have tried it yesterday & is not working this way.
  • sebieire
    sebieire over 2 years
    That'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 with list_display=['field_name',...]. And that does exactly what I explained above. I am on Django 3.2.
  • saadat ali
    saadat ali over 2 years
    I 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
    saadat ali over 2 years
    may be your method work for non image fields only.
  • sebieire
    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.