How do you validate the data that comes into a serializer?
Way back at the beginning of this course, I created Serializers for the course and 0:00 review models those Serializers were fairly simple, 0:03 thanks to Rest framework's model Serializer. 0:06 If you looked over the models in the course's app, you might have noticed 0:09 that there is a unique together in the review models metaclass. 0:12 The unique together means that a reviewer with a specific email address 0:16 can only review a specific course once. 0:19 If you were to try to create two reviews for a course and use the same email 0:22 you'd get back an error saying that course an email must be unique. 0:25 REST rust framework model Serializer automatically sets a validate or 0:28 on the Serializer to match what we have on the model. 0:32 How then, would you specify your own custom validation on a serializer? 0:35 It's very similar to how you handle custom validation on a Django model form. 0:39 Currently the rating field on the review model accepts any integer. 0:43 I think I'd like the ratings to be an integer between one and five. 0:47 Let me jump into Workspaces and build it. 0:49 since I'm only trying to validate a single field, 0:53 I'm gonna solve this with field level validation. 0:56 Okay, so what I'm gonna do is I'm going to add a new method to my ReviewSerializer. 1:00 And I'm gonna make sure and come outside of class. 1:07 Meta. 1:11 That has bit me a couple of times. 1:12 And I'm gonna name this one, validate rating. 1:15 So just like with field level validation for forms, 1:18 you do a set of clean field name, you do validate field name, and 1:23 then this is going to get an argument named, value and 1:28 that's going to be the value that's passed then to the API For that field. 1:30 All right, so if value in range one through six, 1:35 then I'm gonna return the value. 1:40 If that doesn't happen, then I'm going to raise serializers.ValidationError. 1:42 And the message I'm gonna send back is, Rating must be an integer between 1 and 5. 1:48 All right cool that sends the information to them. 1:56 Now this isn't a political to this current thing but 2:00 if you have a field that is not required like for 2:05 instance yeah this comment field here because we have a link it was true so 2:08 it's not required Then any validator applied to that field 2:13 won't fire unless that field is in the provided data. 2:18 So somebody comes and they post a review that doesn't include comment 2:24 then validate comment would never run. 2:27 So all right just a thing to be aware of if you're like why 2:29 isn't my validation working. 2:32 All right so come back over here to postman and 2:34 I want to post a new a new review. 2:37 So the course I want to put this on is course number two which is Python testing 2:41 my name is Kenneth my email 2:45 not emaile is firstname.lastname@example.org. 2:50 And I'm just gonna give a rating here. 2:55 I really like this course, I think it's an amazing course. 2:57 I want to put in a rating of 20 because it's just it's the best course. 2:59 I'm gonna send, and looks like my rating has to be an integer between 1 and 5. 3:05 I don't have any other choices. 3:10 All right, well, I mean, 3:14 if the best I can do is 5 then I'll give it a 5 because it's what it is. 3:15 And send, all right, cool. 3:21 So that's awesome that that works, that enforces my 5. 3:23 Now, there is one of the thing to know about and 3:27 this applies more to something like this where you've Explicitly named a field and 3:30 you can add in a rule here that's like validators equals and then a list. 3:36 I'm gonna take that out because I don't need it and 3:41 that will list all the functions or custom classes or 3:43 whatever that you want it to apply those validations to that field. 3:47 So you don't have to do validate whatever. 3:51 Maybe you need to do two or 3:55 three different validations On the field a couple of or a combination of things. 3:55 And if that's the case then you can just provide the validaters key argument 4:00 to the field when you create the field. 4:05 Field level validation is up and running as usual or provide a link to 4:10 the Serializer validation documentation and the teachers notes. 4:13 Something I didn't cover though, Is validating the serializer as a whole. 4:17 Field level validation only validates a single field, like the rating field. 4:21 Sometimes you need to validate multiple fields together, like maybe for user 4:26 registration, where you'd have a password field, and a password confirmation field, 4:29 and you'd want to make sure that the two fields were equal to each other. 4:33 This is covered very well in the Rest framework documentation. 4:36 So check the teacher's notes when you need to implement this yourself. 4:39
You need to sign up for Treehouse in order to download course files.Sign up