django admin site change the display label of field
Question:
I hope the title is enough to understand what my issue is, I just want to change the default label Fathers Lastname: into Lastname without changing the table field name in the models
This is my models.py:
class ParentsProfile(models.Model):
Fathers_Firstname = models.CharField(max_length=500,null=True,blank=True)
Fathers_Middle_Initial = models.CharField("Middle Initial",max_length=500,null=True,blank=True, help_text="Father")
Fathers_Lastname = models.CharField(max_length=500,null=True,blank=True)
Answers:
Yes, you can change the label name by adding the first postional attribute to the field.
use the code below:
Fathers_Firstname = models.CharField("Lastname",max_length=500,null=True,blank=True)
try adding verbose_name
Fathers_Firstname = models.CharField(verbose_name="Lastname",max_length=500,null=True,blank=True)
You can create the custom column "lastname" with lastname()
and can rename it with @admin.display as shown below:
# "admin.py"
from django.contrib import admin
from .models import ParentsProfile
@admin.register(ParentsProfile)
class ParentsProfileAdmin(admin.ModelAdmin):
list_display = ('lastname',) # ← "lastname()" needs to be assigned
# ↓ Displayed
@admin.display(description='Lastname')
def lastname(self, obj):
return obj.Fathers_Firstname
I hope the title is enough to understand what my issue is, I just want to change the default label Fathers Lastname: into Lastname without changing the table field name in the models
This is my models.py:
class ParentsProfile(models.Model):
Fathers_Firstname = models.CharField(max_length=500,null=True,blank=True)
Fathers_Middle_Initial = models.CharField("Middle Initial",max_length=500,null=True,blank=True, help_text="Father")
Fathers_Lastname = models.CharField(max_length=500,null=True,blank=True)
Yes, you can change the label name by adding the first postional attribute to the field.
use the code below:
Fathers_Firstname = models.CharField("Lastname",max_length=500,null=True,blank=True)
try adding verbose_name
Fathers_Firstname = models.CharField(verbose_name="Lastname",max_length=500,null=True,blank=True)
You can create the custom column "lastname" with lastname()
and can rename it with @admin.display as shown below:
# "admin.py"
from django.contrib import admin
from .models import ParentsProfile
@admin.register(ParentsProfile)
class ParentsProfileAdmin(admin.ModelAdmin):
list_display = ('lastname',) # ← "lastname()" needs to be assigned
# ↓ Displayed
@admin.display(description='Lastname')
def lastname(self, obj):
return obj.Fathers_Firstname