Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Adding a 'ReviewModel' with field validation
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Offscreen, I've created a new app called review. You've seen me do this enough now that hopefully you can manage it yourself.
0:08
As a quick reminder, you'll use the start app management command to create the app and then don't forget to add it to the installed apps
0:15
configuration in settings.py. This is my new review model. It has both field validation and database validation constraints.
0:28
Let's start with the field validation. On line 3, I've imported the min value validator and the max value validator from the Django
0:38
core validators module. I'm applying these two validators to the rating field to ensure that
0:45
our book review can only be rated from a min value of one to a max value of five.
0:51
Every model field type supports the validators attribute which takes a touple containing one or more validation that you wish to apply to the field.
1:01
In this case I'm applying two validators. One that ensures the minimum acceptable value is one
1:06
and one that ensures the maximum acceptable value is five. And that's all you need to do field validation.