51

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")
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54

3 Answers3

104

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)
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • works great, is there some page in the docs where I could read more about this and other features like it? – Francis Yaconiello Mar 14 '12 at 19:57
  • Unfortunately, this destroys the possibility to sort by this column – cxxl Jun 03 '21 at 18:33
  • @cxxl I found [this](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#:~:text=Usually%2C%20elements%20of%20list_display%20that%20aren%E2%80%99t%20actual%20database%20fields%20can%E2%80%99t%20be%20used%20in%20sorting%20(because%20Django%20does%20all%20the%20sorting%20at%20the%20database%20level).) – CSSer Jul 16 '21 at 08:45
8

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

Marco
  • 1,377
  • 15
  • 18
0

Inside your models.py model class you can simply do as follows for (usually) any field:

your_field = models.CharField(max_length=123456789, verbose_name="WOWYWOWY!!!")
sebieire
  • 418
  • 2
  • 11
  • Completely irrelevant answer, renaming a column in admin panel has nothing to do with verbose_name in models – saadat ali Jan 05 '22 at 06:05
  • 2
    Have you actually tried this before down voting the answer? This offers an alternative solution to the original question. Please check first and revisit. – sebieire Jan 06 '22 at 10:01
  • Yes bro, I have tried it yesterday & is not working this way. – saadat ali Jan 06 '22 at 10:08
  • 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. – sebieire Jan 06 '22 at 10:29
  • 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(''.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 Jan 06 '22 at 11:21
  • may be your method work for non image fields only. – saadat ali Jan 06 '22 at 11:21
  • @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. – sebieire Jan 06 '22 at 16:27
  • 1
    it works with character like fields – Mohammed Jaseem Tp Jan 05 '23 at 17:44