RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Validation with view models
Lecture: View model classes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 I'd be pretty happy to call this view model done but in a real system, we'll have many of these view models for different situations,
0:09 there might be an update auto view model that's different than a create auto view model and of course maybe we want to create users and dealerships
0:17 and like there is just a lot of stuff in practice where you probably end up with more than one of these, maybe more than ten these,
0:24 so let's do a little bit of work to make this easier to add new view models;
0:27 so over here, I'm going to add the name will give it all away a base view model. So we're just going to have a class called view model base let's say,
0:37 I will just put an empty representation for a minute we'll come over here and say you know what, this thing is going to be one of those
0:45 and let's go ahead and call the base initializer, and things like this like the errors, pretty much any view model that is worth having
0:58 is going to do some kind of creation and error checking and validation, there's probably a few that don't, but the majority of them will
1:05 so let's go ahead and give it, like push the concept of errors and handling error messages down to the base
1:11 all right, so I got that one and then this for now, there are errors with your request or something like that
1:19 make this little general, and then this can go down there as well; great, so now any view model we want
1:25 automatically knows how to keep track of its errors and report all the errors that it has encountered. So now it's really just down to the car stuff,
1:32 getting the values we need, validating them, I guess we probably could leave that empty ; But just take the default and we'll put the values in here,
1:43 test them and of course don't create the car if it's not possible or valid,
1:50 let's just test one more time that our new base class here is doing this job. Okay, so maybe we want to omit the name, try to create one,
2:00 no, your name is required, so obviously we want the errors field to store it in, and the errors message worked, so we put this back
2:07 do it again, now we've created a new car, perfect, so I think we're done with our view model section.

Talk Python's Mastodon Michael Kennedy's Mastodon