Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: View models demo (intro section)

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now recall over here, we had to pass this structure data to our template and for our template to work we really needed
0:08 email, password confirmation and error always provided. Moreover, we had some validation that we were going to do,
0:15 right here, like that the confirmation matches, and we had this bit of code we had to write to pull this stuff out of the POST dictionary
0:24 to get the data loaded from the POST into our method. All that stuff should really be done somewhere else and unified into a single, consistent style.
0:35 So I want to introduce you to this concept of view models, now view models means many things in different situations,
0:42 what I am talking about here is we have specific classes that bundle the validation and the data,
0:49 and how to get and convert that data to and from strings in the forms and so on. And they are typically, not a 100% of the time but very often
0:57 there is a single view model dedicated to a single Chameleon template, single view.
1:03 Because what's in that view is often similar but not the same as other things. They can create data the form might need to show itself,
1:12 so for example maybe you have a form and you want to say "select the state" and you have a drop down full of states,
1:18 well where do those stated come from? They would come from the view model. As well as the validation, but most importantly
1:23 they provide consistent data exchange, so I am going to create a folder over here,
1:29 called viewmodels, and just to save us a little trouble so it won't be a major deal... but let's put a few things in a common base class,
1:40 so we can call this whatever we want, so let's just call it ViewModelBase, make it really obvious,
1:44 and it's going to have basically one method to dictionary. And what we are going to do is we need to remember,
1:51 over here we need to return literal dictionaries, if you try to return a custom type you would see it's not going to like it so much,
1:57 it's has to be a dictionary. So what we need to do is take whatever class we are working with and turn its data into a dictionary.
2:04 For custom types this is pretty simple, we can say self.__dict__ and a lot of times we can get away with passing this, if this is insufficient,
2:11 you can overwrite this method and then return the data in whatever for you want, sometimes that will be necessary.
2:17 Now, remember what we are trying to work on is we are trying to work on a register, so let's create a register_viewmodel, like this,
2:26 and it's going to be a class called RegisterViewModel, it's my naming convention, you can use whatever you like,
2:33 we are going to derive from ViewModelBase, and we'll let PyCharm pull that in, here we go, we could import that, great,
2:41 what are we going to do, it's going to need to have some data, remember, what do we have up here, we have... let's just actually grab this here,
2:49 and say "well, we want to store this data and we want to make sure we have this", so let's just put it into __init__
2:57 and down here obviously this is not what we want, we want to say "self.", let me just clean this up.
3:07 OK, now we have this data here, and if we call to dict, we are actually going to end up returning a dictionary with email as a key,
3:15 because remember, __dict__ is basically where the fields are stored, the attributes are stored, and we are going to have the value of None,
3:22 let's actually start using this straight away, so if I come up here, we can say "I'd like a view model to be one of these",
3:31 we could import that at the top, and down here we'll just return dict, remember that comes from the base view model.
3:38 Now, let's go and run this and see that everything still works. If I go up here and click on register, it should provide that information,
3:49 remember when it didn't it crashed, boom, there it is, email, password, confirmation, and so on. OK, so now we've got a little bit of this managed,
4:00 this little bit of what that view needs to display itself, managed right here,
4:04 we could even inline this, like allocate this and call to dict straight away. Next up, let's go down here and work on this part.

Talk Python's Mastodon Michael Kennedy's Mastodon