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?
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?
On
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']
You can create custom columns.
For example, there is
Personmodel below:Now, you can create the custom columns "my_name" and "my_age" with
my_name()andmy_age()and can rename them with @admin.display as shown below:Then, MY NAME, MY AGE and the values of "name" and "age" fields are displayed as shown below:
Of course, you can assign "name" and "age" fields to
list_displayin addition to the custom columns "my_name" and "my_age" as shown below:Then, NAME, AGE and the values of "name" and "age" fields are displayed as shown below: