[Django]-Verbose name for admin model Class in django

49👍

class User(models.Model):
    fname = models.CharField(max_length=50, verbose_name = 'first name')

    class Meta:
         verbose_name = "users"

Source: https://docs.djangoproject.com/en/2.1/topics/db/models/#meta-options

18👍

verbose_name and verbose_name_plural both the properties of Meta class are very important to modify the default behaviour of Django to display the name of our models on Admin interface.

You can change the display of your model names using on Admin Interface using verbose_name and verbose_name_plural properties and model fields names using keyword argument verbose_name.

Please find the below 2 examples.

Country model:

class Country(models.Model):
    name = models.CharField(max_length=100, null=False, blank=False, help_text="Your country", verbose_name="name")
    userid = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return f"Country {str(self.id)} - {self.name}"

    class Meta:
        verbose_name = "Country"
        verbose_name_plural = "Countries"

If you will not specify verbose_name_plural then Django will take it as Countrys which does not look good as we want it as Countries.

This better fits in the following type of Model.

Gender model:

class Gender(models.Model):
    name = models.CharField(max_length=100, null=False, blank=False, help_text="Gender", verbose_name = "name")
    userid = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return f"Gender {str(self.id)} - {self.name}"

    class Meta:
        verbose_name = "Gender"
👤hygull

9👍

You could add a “verbose_name_plural” to the “Meta” class too.

6👍

To alter admin model without polluting the model itself, you can utilize a proxy admin model, like this:

# admin.py
from . import models

class Users(models.User):
    class Meta:
        proxy = True

class UsersAdmin(admin.ModelAdmin):
    ...


admin.site.register(Users, UsersAdmin)

0👍

ConfigAbility._meta.verbose_name = ‘config ability’

ConfigAbility._meta.verbose_name_plural = ‘config ability’

I did explore this, but don’t know whether it’s the thing you need. I put those codes in class ConfigAbilityAdmin in Admin.py. Then, the result:

enter image description here

With this approach, you don’t need to config Meta method in model class, especially when model class’s code is generated from inspectdb…

Leave a comment