Home > other >  How do I change the colum name of model in Django Admin?
How do I change the colum name of model in Django Admin?

Time:08-22

My admin.py file

from django.contrib import admin
from . models import contactEnquiries

class ContactAdmin(admin.ModelAdmin):
    list_display =  ['cn_name', 'cn_email', 'cn_number']

    @admin.display(description='Name')
    def cn_name(self, obj):
        return obj
    

admin.site.register(contactEnquiries, ContactAdmin)

I want to display the column titles as - Name, Email, Number instead of 'cn_name', 'cn_email', 'cn_number' in the admin panel. I tried the above code but I am not sure how to do it? Can anyone please help?

CodePudding user response:

You can use verbose_name in your model itself without affecting your database or any other function. Like this column_name = models.CharField(verbose_name="name you desire") and that will change the name of your column in django admin for you.

In your case you use the following code:

class contactEnquiries(model.Models):
      cn_name = models.CharField(verbose_name="Name", your other config)

      #your all other columns here and you can user verbose_name in all your model columns.

You can read the documentation here: https://docs.djangoproject.com/en/4.1/ref/models/options/#verbose-name

CodePudding user response:

from django.contrib import admin
from . models import contactEnquiries

class ContactAdmin(admin.ModelAdmin):
    list_display =  ['cn_name', 'cn_email', 'cn_number']

   
    def cn_name(self, obj):
        return obj.cn_name
    
    cn_name.short_description = 'Name'

    def cn_email(self, obj):
        return obj.cn_email
    
    cn_email.short_description = 'Email'


admin.site.register(contactEnquiries, ContactAdmin)
  • Related