O'Reilly logo

Django Unleashed by Andrew Pinkham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required