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