Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Using a viewmodel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now with this view model base in place let's go and actually see how we might use it. This is what is actually calling for
0:10 the view model idea the most. But let's start with something simpler. Just this index, like all we want to do
0:15 is simply show the user details on the screen. So we've got to get the user_id and potentially, the user as well. So, let's go over here to the account
0:24 and we'll have a index view model, something like that. We could call it AccountIndexViewModel but I'll leave it up to you how you want to name these.
0:34 And it turns out to be really straightforward, what we need. We'll come over here and say IndexViewModel. And it's going to derive from ViewModelBase.
0:43 So let's import that. And actually, I'd like to name it slightly differently. Something like that, so it doesn't conflict
0:54 with our class name which what we actually care about. So we're going to have another initializer here
1:00 and it's not actually going to take any arguments 'cause we're not getting any arguments here or anything like this. So we're going to come down there
1:08 and the first thing we have to do is PyCharm miscellaneous add a call to super so it'll write this or we could write it ourselves.
1:15 And then, let's look at what we're exchanging. Well, this part right here we have to get an exchange for that and that's actually happening already.
1:23 We also have to go over and get the user. So let's try that. We'll come over here to our IndexViewModel and well say self.user is user_service
1:32 got to import that. What do we pass here? Remember the base class is already setting that up and that happened already there
1:39 so we'll say self.user_id, super! And this may or may not come through Now what else do we have to do? Let's actually go back and find out.
1:49 So we're going to go over here and create a vm which is a IndexViewModel, like so. Create one of these. Now this already happens, and so does this.
2:01 So all we have to do is go down here and say vm.user and let's go ahead and check that actually the query went down to the database
2:07 and found a real user, 'cause maybe somehow they deleted their account or whatever. All right, if we can't actually get a user
2:14 from the database they have to go login. Look how that got simpler, that's nice. Now remember, over here we had to do this as well.
2:22 Now what do we return? All the fields and stuff contained within this view model. There is this extra error thing
2:29 but it doesn't matter, we're just going to not use it and we'll just say to_dict, and that's it! Look how much cleaner this is become.
2:37 And as we add more information to be exchanged this part here gets no more complicated. It's this data exchange bit, and this is pretty simple
2:46 but we're going to get more interesting stuff as we move down the line here in this file. But already this has gotten better.
2:52 Well, I say it gets better, it's actually going to be better if it really continue to work but have a better style serve.
3:00 All right, it looks like it restarted. Let's go and see that it still works. All right we're still logged in, ta-da!
3:06 There we go, so we got up here, the user_id info we got here, the actual user object and then we pass some other information
3:13 that we technically don't need. Cool, huh? So here's a simple version of using a view model.

Talk Python's Mastodon Michael Kennedy's Mastodon