Django Verbose Field Names

By: Dusty Arlia
Published on Sunday, March 16, 2014, 04:05 PM
Last Updated on Tuesday, July 07, 2015 at 10:47 PM
Total Updates: 2

Each Django model field type takes an optional first positional argument: a verbose name (except for ForeignKey, ManyToManyField and OneToOneField field types.) Here is an example:

#the verbose name is "person's first name"
first_name = models.CharField("employee's first name", max_length=50)

ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class. When using these field types, use the verbose_name keyword argument:

school = models.ForeignKey(School, verbose_name="school of employment")
classes = models.ManyToManyField(Classes, verbose_name="list of classes taught")
salary = models.OneToOneField(Salary, verbose_name="yearly salary")

Don't capitalize the first letter of the verbose_name. Django will automatically capitalize the first letter where it needs to.

If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces. Here is an example:

#the verbose name is "first name":
first_name = models.CharField(max_length=30)

Comments:

Ad:

Ad: