Home > Net >  In Django, what's the difference between verbose_name as a field parameter, and verbose_name in
In Django, what's the difference between verbose_name as a field parameter, and verbose_name in

Time:01-03

Consider this class:

class Product(models.Model):
  name = models.Charfield(verbose_name="Product Name", max_length=255)

  class Meta:
    verbose_name = "Product Name"

I looked at the Django docs and it says:

  • For verbose_name in a field declaration: "A human-readable name for the field."
  • For verbose_name in a Meta declaration: "A human-readable name for the object, singular".

When would I see either verbose_name manifest at runtime? In a form render? In Django admin?

CodePudding user response:

The verbose_name in the Meta deals with the name of the model, not field(s) of that model.

It thus likely should be 'Product', not 'Product Name':

class Product(models.Model):
    name = models.Charfield(verbose_name='Product Name', max_length=255)

    class Meta:
        verbose_name = 'Product'

This thus specifies the table name in the model admin, whereas the verbose_name of the field(s) will show up in ModelForms and when you display or edit records.

CodePudding user response:

verbose_name in a field declaration is set when the name of that field (here we mean the name) is not enough for the user to explain what that field is exactly. For example, maybe you want to provide a more complete description of the name to the user, suppose you mean a short name. So it is better to set verbose_name equal to the "short name". verbose_name in a Meta declaration is set when you want to show your objects individually in a different way to the user in the admin panel. Of course, we also have verbose_name_plural in the meta class. It is used when Django cannot correctly recognize the plural form of a word. Django shows word endings by placing s in plurals, but this is not always true. For example, imagine you have a model called Child. Well, now it is better that you set the verbose_name_plural value equal to "children" in the meta class. When you use a language other than English in Django, the above description is more useful.

  • Related