Customizing field labels

On the admin site's edit forms, each field's label is generated from its model field name. The algorithm is simple: Django just replaces underscores with spaces and capitalizes the first character, so, for example, the Book model's publication_date field has the label Publication date.

However, field names don't always lend themselves to nice admin field labels, so in some cases you might want to customize a label. You can do this by specifying verbose_name in the appropriate model field. For example, here's how we can change the label of the Author.email field to e-mail, with a hyphen:

class Author(models.Model): 
    first_name = models.CharField(max_length=30) 
    last_name = models.CharField(max_length=40) 
    email = models.EmailField(blank=True, verbose_name ='e-mail')

Make that change and reload the server, and you should see the field's new label on the author edit form. Note that you shouldn't capitalize the first letter of a verbose_name unless it should always be capitalized (for example "USA state"). Django will automatically capitalize it when it needs to, and it will use the exact verbose_name value in other places that don't require capitalization.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset