Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Course conclusion
Lecture: viewmodels

Login or purchase this course to watch this video and the rest of the course contents.
0:00 When we were accepting user input we saw that there's a lot of validation a lot of data exchange and other things we have to do.
0:07 And that's not so much fun. And the exact data and the format of the data that we want to pass back and forth also takes a lot of work.
0:15 So we decided to apply this design pattern called view models. And the idea is the view models are tightly tied to the HTML form that
0:24 they're exchanging data with. They know how to get the data from the form and provide it back to it or things like drop downs and where it needs it
0:31 and roundtrip it and so on. It also knows how to do all the validation. And that can be basic validation like Hey, we require that you type in an email
0:38 address here, or, This has to be a number, or, It has to bigger than five, and so on. But it can also be richer, deeper validation like
0:45 Sorry, you can't register here. You can't create an account with that email 'cause that email address is already tied to another user.
0:52 Maybe you should just try to login and reset your password. And it's both made testing easier 'cause we can test the view models directly
1:00 and just do validation there. Don't have to involve Flask and HTTP infrastructure, and so on. It also made our action methods
1:06 and our view methods much simpler, right? They don't get larger and more complex as we add more validation. Just the one place where validation goes
1:15 the view models, does. But that's okay. That's its job. Strongly recommend this pattern. There's some other add-on libraries
1:22 for Flask and stuff that you can use for forms and whatnot. But I really like this pattern because you have complete control over it
1:27 and it's super straightforward and good for testing.


Talk Python's Mastodon Michael Kennedy's Mastodon