Making fields optional
After you play around with the admin site for a while, you'll probably notice a limitation-the edit forms require every field to be filled out, whereas in many cases you'd want certain fields to be optional. Let's say, for example, that we want our Author
model's email
field to be optional-that is, a blank string should be allowed. In the real world, you might not have an e-mail address on file for every author.
To specify that the email
field is optional, edit the Author
model (which, as you'll recall from Chapter 4, Models, lives in mysite/books/models.py
). Simply add blank=True
to the email
field, like so:
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.