Adding a CMS to Your Flask Web App Transcripts
Chapter: Saving content to the DB
Lecture: Creating the Page class

Login or purchase this course to watch this video and the rest of the course contents.
0:00 in orderto have a page table in our database into store pages in it using seek Welcome me. What we need to do is create a class just like the user
0:09 thing. And as is generally the case here, it's easier to copy and modify one than start totally from scratch.
0:15 So gonna copy and paste. It's gonna be pages plural. This will be pages, but this will be singular. Page. The class represents one.
0:26 The table holds many. What a bunch stuff. Let's drop those out for a second. Hardly gonna stories. Well, there's two types of data.
0:34 There's what I would call admin data, and there's a direct data. Who knows? It's not quite the right term,
0:42 but we have things that are essential to the page itself. And these were the title your elf in the content to.
0:54 These are what we want when we get the page back. We need to have these, but it's also useful have things like an i D
1:00 So we can always refer exactly back to that one as well as it is Nice toe. Have additional information I find whenever I'm working with databases.
1:10 Eventually, I want to know a this thing that's here. Why is it here? How did it get here and win?
1:16 So we're gonna have to columns or two pieces of data here that allow us to
1:20 store that will never really use it for interacting with users right in our Web app But it's gonna be for as we work with the right so wannabe win it
1:29 that when it was created So that created date and the other one is going to
1:35 be the creating user. This is who created it will probably just store like their email, not an actual relationship back to the user table.
1:44 That way, in case we don't leave the user, we still have a history of Hey, here's the email. The person created this page.
1:50 It's gonna look a lot like this. They're all gonna be sequel Alchemy columns, as is the case. So let's start by dropping those in there great.
2:02 And then we get to say what type they are. So this is gonna be a sequel. Alchemy, Don date time we a sequel,
2:08 Alchemy string and pretty much all of the rest of strings as well. Next thing we want to ask is what is required and what has to be unique
2:19 So let's say the contents is not necessarily required, but that the euro must be there so we can say no level is false for
2:28 the ones that are required already enforcing this in the website. But let's also enforce it in the database. Like so for a created date.
2:38 We want to require it. But instead of saying you must set the value, let's say give it away to automatically figure out when it was created and just do
2:46 that we don't have to deal with it. Sequel Alchemy will do it for us the way we do that as we set a
2:50 default to a function which is daytime dot date time dot Now we want to pass the now function. Now be very careful Here I hit.
2:59 Enter watch what happens? It writes the parentheses. What this would do would be set the default to be the time the program started
3:06 Don't want that. We want the time when this individual page it was saved So we've got to give it the now function,
3:13 not the value of now. When it starts down here for the girl we don't want to pages to be ableto have the same mural which paid,
3:22 Would you show when they try to go to that one? So we don't Here we can say this is unique is true. Don't add an index for uniqueness.
3:30 We also want to know which things we might Queary by. So we're not gonna come and say Give me the page with the title of this
3:38 or the contents of that That would be weird. We probably won't even say Give me all the pages created by such and such you there. But when we coming,
3:46 look for a page we go to a u. R l and say, Do we have a page for that? So we want to make sure that for those types of things,
3:52 we have an index in there as well. If we have an index and you have lots of data, this could make it 1000 times faster or maybe more.
4:02 It's tremendously important to have indexes for your queries. One of the cheapest, quickest,
4:09 easiest things you can do to make your website much faster. That's true here. Now it's also probably true appear for the create date.
4:19 It's very unlikely wanna say, Give me the page created on this day or at exactly this time, but it is probably the case.
4:25 We want to show the newest ones first in our admin, and we'll therefore order by the created eight. And having an index makes that faster as well.
4:34 Okay, well, this looks like we've probably got what we need for our page The final thing to do to make everything work is remember,
4:43 we have this all models, and in order for something to show up here, we gotta put it in and it's super easy over here and say not page pages
4:53 and drop that in before I run this. Let's go over here and on through a quicker refresh notice. There's packages, but no page.
5:03 Don't really want that much information. There's a package, but no page mint. Now let me just run the website.
5:09 It ran without crashing. That's actually a good couldn't sign, but let's refresh. Here we go because our package was loaded,
5:18 our pages load. It was seen. Therefore, sequel alchemy new to create it, and it has all the stuff we specify.
5:24 An auto incriminating primary key created date. You will notice the ones with the blue vertical things.
5:32 Those have indexes. So here's our indexes and so on. All right, well, there's no data in there yet, but we're off to a really good start,


Talk Python's Mastodon Michael Kennedy's Mastodon