25

Is it possible to create a verbose name for the actual Class model?

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

So in the admin panel it will be referenced by its verbose name and not 'user' ?

David542
  • 104,438
  • 178
  • 489
  • 842

5 Answers5

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

solartic
  • 4,249
  • 3
  • 25
  • 26
  • 1
    and they are translatable http://stackoverflow.com/questions/2462905/is-it-possible-to-change-the-model-name-in-the-django-admin-site – Stefano Aug 30 '12 at 01:09
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"
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
hygull
  • 8,464
  • 2
  • 43
  • 52
  • *title = models.CharField(max_length=25 ,verbose_name=_("Quiz Title"))* what it will do in this case and where am I going to use it. – Lord-shiv Jun 05 '21 at 06:45
9

You could add a "verbose_name_plural" to the "Meta" class too.

glenfant
  • 1,298
  • 8
  • 9
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)
lehoang
  • 3,577
  • 1
  • 15
  • 16
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...

c0ngth4nk
  • 11
  • 1