pythondjangodjango-modelsdjango-forms

What's the purpose of verbose_name and help_text in Django model fieds?


I'm wondering what's the purpose of setting verbose_name on Django model fields and how it's different from help_text. How are these being used by Django?


Solution

  • These names are used in the Django admin pages. So instead of using the name of the model class, it will use the verbose_name of that model to make messages that are less "mechanical".

    Furthermore you can use these in your own projects. For example if you want to obtain the verbose_name of a model you do not (statically) know, you can retrieve it with:

    model._meta.verbose_name

    This can be useful to write generic messages. You can write a warning message like:

    Are you sure you want to delete the {{ obj._meta.verbose_name }}?

    If your model is PizzaTopping, then the default verbose_name is 'pizza topping'. It is more human friendly to write "Are you sure you want to delete the pizza topping?" than "Are you sure you want to delete the PizzaTopping?".

    You can for example retrieve the verbose_name of a field with:

    model._meta.get_field('some_field').verbose_name

    Django's ModelForm [Django-doc] furthermore use by default the (capitalized) verbose_name and the help_text as label and help_text in the form. If you thus render a model form, and you do not tweak the settings, it will take the verbose_names of the model fields as labels, and the help_text will show up over that field.