Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbose name for admin model Class in django

Tags:

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' ?

like image 979
David542 Avatar asked May 11 '11 04:05

David542


2 Answers

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 "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 "Gender " + str(self.id) + " - " + self.name

    class Meta:
        verbose_name = "Gender"
like image 36
hygull Avatar answered Sep 21 '22 09:09

hygull


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

like image 58
solartic Avatar answered Sep 20 '22 09:09

solartic