#100DaysOfWeb in Python Transcripts
Chapter: Days 37-40: Introduction to Pyramid framework
Lecture: Adding the data models

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, in order to make this web application interesting
0:02 we're going to have a real data model
0:04 because building the data model
0:06 is not really part of this lesson.
0:08 We've already spent a long time on SQLAlchemy
0:10 and modeling data.
0:11 I'm going to go and just take some pre-built data
0:14 and put it in here.
0:15 Some SQLAlchemy models, and so on.
0:17 What we need, I don't need.
0:19 It would be nice to have interesting data.
0:22 Not just random fake stuff
0:23 but somewhat realistic data.
0:26 So, let me introduce you to a place called Mockaroo.
0:29 Mockaroo is a cool website
0:30 that lets you generate these schemas.
0:33 I think I might even have some saved schemas here.
0:36 Yeah, this build tracker payments. Let's open that one.
0:38 So, check this out. It lets you create the schema that says
0:41 can have an ID, which is a auto-incrementing, basically, id.
0:45 We had a created_date, that is going to be
0:48 between those two years in this format of a description
0:52 and it can actually come from retail items.
0:54 You know, like shopping, electronics, whatever.
0:57 Some numbers with min and max, paid amount, and so on.
1:00 And then you can go and just generate this
1:02 or preview it, if you wish. And look at that.
1:04 Isn't that cool?
1:05 So, for the data model, what I did is I generated
1:08 a bunch of users and a bunch of payments
1:11 bills, rather, like this.
1:13 And they're going to have paid some
1:15 not all of them, and so on.
1:17 I'm going to copy over a little bit of the data model
1:20 and then we'll talk through that real quick.
1:22 Like I said, we've already spent so much time
1:24 in the SQLAlchemy chapter.
1:25 I don't want to spent another half hour
1:27 modeling that out here.
1:28 I just want to show it to you.
1:29 You can use it, if you want to go back
1:30 and study the SQLAlchemy section, go for that.
1:33 So I'm going to come over here and paste three folders.
1:36 I'm going to paste this one called db.
1:39 And that has just the data generated from Mockaroo.
1:42 Okay, it has these two pieces right here.
1:44 Over in bin, it's just going to
1:46 fix those errors in a moment
1:48 but it's just going to go and load up that JSON file
1:50 and then put them in the database.
1:53 So, it's going to run, load that data
1:56 put it into our SQLAlchemy models, and save them.
1:59 Speaking of SQLAlchemy
2:00 down here we have our database session.
2:04 Again, it looks like I have to add that requirement
2:06 and change that name real quick.
2:09 And this is just like what we did before.
2:10 We have this initialization function we call
2:12 we're going to connect to a SQLite database
2:15 that lives in this folder.
2:16 Our repository is the pre-built queries
2:19 that we might run, like, get me a particular user by id.
2:23 And then here, we have our SQLAlchemy models for bill
2:27 and our SQLAlchemy models for users.
2:29 Let's tell it that that's saved.
2:31 Let's fix these errors real quick.
2:32 So, this'll be bill.
2:35 Alright, looks like we've got that all fixed up.
2:38 The other thing we need to do is to add SQLAlchemy
2:41 and this one right here
2:42 which is actually python-dateutil
2:46 as the two dependencies for our project
2:48 'cause see, there are new dependencies
2:50 we're going to depend upon.
2:51 So, we'll have sqlalchemy and we'll have python-dateutil.
2:56 Tell that all of these things are spelled correctly.
3:04 Just run that again.
3:09 Alright, it's back and working.
3:10 The other thing is
3:11 I'm not sure what's wrong with SQLAlchemy
3:12 but that is okay, should be okay.
3:16 So, last thing to do to get our data up and running is
3:18 we want to import it into our database.
3:21 So, we're going to call this at the start of our app
3:22 just to load the starter data.
3:24 And notice the first thing that it does
3:26 it says if there's already any data here
3:28 we're not going to import anything.
3:29 We're just going to bail out.
3:30 So, I'm going to just add that to the main startup here.
3:35 So, we'll create a function called an init_db
3:37 put that down here.
3:39 And let's just start from existing code.
3:41 So, we'll have our os, which we're going to use
3:43 to find the path to that db folder.
3:46 And of course, we're going to import that
3:49 and we're going to import this load-based data.
3:52 And that should do it.
3:53 Let me make sure this got added correctly, from.
3:57 Let's do it like this. There we go.
4:01 Alright, so now when this starts up
4:02 it should call this.
4:03 Let's see one more time, does it run?
4:04 As you can see, it's now using that database.
4:07 And if we look in here, and we let it refresh
4:11 we now have our bill tracker SQLite database.
4:15 And I guess I could look at it over here, real quick.
4:22 Now, if we run it, we now have, see
4:24 some real-looking users.
4:27 They're not real, right?
4:28 They're made up by Mockaroo
4:29 but they're somewhat real looking.
4:31 So, it looks like our data model is in place
4:33 and up and running.