Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Field validators and model constraints
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In the overview, I give you a quick rundown on the two mechanisms used for ensuring clean data in your database.
0:07
The first way is through field validators. These are created through adding properties to a field in a model and typically limit what
0:15
can be stored in the field. You can specify limits like min and max values, min and max length, as well as
0:21
validate the data conforms to a particular pattern like say email addresses, URLs, IP addresses, a list of numbers or through a regular expression.
0:31
All this validation happens when you save your model object and a validation error exception
0:35
is raised if the data in the model isn't compliant with your validators.
0:41
The 2nd mechanism for managing your data formats is putting constraints on the model itself.
0:46
This is enforced by the database and is usually used to ensure constraints across multiple fields.
0:52
For example, if the combination of two fields is supposed to be unique, you can do that through a constraint. Just like the field validators,
1:01
An exception is thrown if you attempt to save something that isn't compliant. Let's create a new app and play with some validation.