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


Talk Python's Mastodon Michael Kennedy's Mastodon