3.4.4 Controlling Model Field Behavior with Field Options
It is possible to pass options to model fields. These field options can constrain behavior or provide defaults to our data, allowing us to write less code later. As model fields are (instances of) Python classes, we are simply passing parameters to the instantiation of field classes.
One of the most powerful options available is the ability to tell Django and the database that certain values are unique. This is particularly useful for slugs, which we know must be unique (SlugField
instances do not default to being unique because of the many options available, as we shall see in Chapter 10: Revisiting Migrations). In the case of our Tag
model, in /organizer/models.py
, we set both the
Get Django Unleashed 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.