Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Viewmodel data exchange

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Few models provide two basic services. They provide data exchange and normalization. And they also provide validation.
0:08 So we saw this way of taking a request and converting it into a dictionary through the view model was super valuable. So let's see how that goes.
0:17 We're going to have this view model based class and all of the models will derive from it it's going to have this constructor.
0:22 And here we're going to initialize the standard shared data maybe everything wants to get a hold request we're going to have an error set just in case
0:29 or at least an error present, not central value but it'll be present in the dictionary as well as what the user ideas and our actual code
0:37 we actually went to the cookie often got that out. So it was passed back through a cookie was automatically set here. Now how do we get these fields
0:44 and all the one set by the derived classes over as a dictionary, super simple we just call self.to_dict.
0:51 So the concrete view models like the registration one and package management one or whatever ones we create
0:58 are just going to set fields than the standard dict is going to have all those fields and it will just pass them along.
1:04 And that's how we do data exchange. Now in the concrete view model so when they derive from the base that are actually modeled for a particular view
1:12 we're going to do something a little bit different. So over here, we have our registration remodeled derived from ViewModelBase
1:18 itself going to have this constructor and it's going to call the super's initialization or constructor that's going to set all the base stuff
1:26 and then it's going to go to the request maybe go to the form, get the name do the normalization and store all of these things right here.
1:34 That way when we return the __dict__ out of the ViewModelBase it's going to have first_name, last_name, email password, full_name
1:41 and all the stuff that the base class set as well. It works like a charm.

Talk Python's Mastodon Michael Kennedy's Mastodon