How to change the label in display_list of a field in the model in django admin?

Question:

I have a model with some fields with a verbose_name. This verbose name is suitable for the admin edit page, but definitively too long for the list page.

How to set the label to be used in the list_display admin page?

Asked By: hl037_

||

Answers:

It might be possible that you use verbose_name the wrong way, and that you should use help_text=… [Django-doc] instead:

from django.db import models


class MyModel(models.Model):
    name = models.CharField(
        max_length=64,
        help_text='here some long help text that this is about filling in the name',
    )

If you really want to use a different one for the list_display, you can work with a property, like:

from django.contrib import admin
from django.db import models


class MyModel(models.Model):
    name = models.CharField(
        max_length=64,
        verbose_name='Long name for name field',
    )

    @property
    @admin.display(description='Short name', ordering='name')
    def name_display(self):
        return self.name

    @name_display.setter
    def name_display(self, value):
        self.name = value

Then in the ModelAdmin you use the name_display:

from django.contrib import admin


@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    list_display = ['name_display']
Answered By: Willem Van Onsem

You can change the description of the headers in a few different ways. "Description" meaning the table headers of the admin list view.

Please see:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

Which describes several methods to customise the headers in the list display view of the admin pages.

Answered By: Swift

You can create custom columns.

For example, there is Person model below:

# "models.py"

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=30)
    age =  models.IntegerField()

Now, you can create the custom columns "my_name" and "my_age" with my_name() and my_age() and can rename them with @admin.display
as shown below:

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_display = ("my_name", "my_age") 
                  # "my_name" and "my_age" need to be assigned

    @admin.display(description='My name')
    def my_name(self, obj):      # ↑ Displayed
        return obj.name

    @admin.display(description='My age')
    def my_age(self, obj):       # ↑ Displayed
        return obj.age

Then, MY NAME, MY AGE and the values of "name" and "age" fields are displayed as shown below:

enter image description here

Of course, you can assign "name" and "age" fields to list_display in addition to the custom columns "my_name" and "my_age" as shown below:

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_display = ("my_name", "my_age", "name", "age") 
                                          # ↑ Here ↑

    @admin.display(description='My name')
    def my_name(self, obj):
        return obj.name

    @admin.display(description='My age')
    def my_age(self, obj):
        return obj.age

Then, NAME, AGE and the values of "name" and "age" fields are displayed as shown below:

enter image description here

Answered By: Kai – Kazuya Ito