Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Saving content to a database
Lecture: Creating the Page SQLAlchemy class

Login or purchase this course to watch this video and the rest of the course contents.
0:00 now, in order for us to save our pages in a redirected data to the database, we have to do basically what we're doing here with users.
0:08 We have to create a class it has to derive from sequel Alchemy Base given a name in the database and give it thes field slash column descriptors from sequel alchemy.
0:18 And as usual, the easiest way to build something similar to this is to make a copy.
0:22 So control C Control V and want to call this page ager pages everyone else's or also the file is the poor won.
0:31 The class name is going to be the single thing cause it represents one of them.
0:35 But in the database again, we'll make it plural because the database holds many of them. Now let's get a bunch of that stuff out of here.
0:42 We'll start from scratch.
0:44 So we have two types of data that we're gonna put in here some alcohol kind of admin, auditing database management type, and then the a central core data of the page.
0:55 Let's do the coordinator. First we have the URL.
0:58 We'll figure out what it is in a second, and we're gonna go over here and have the title of the page and the contents of the page want to set.
1:06 Those just wants us to add another line there.
1:08 So we have that, then over here, any time I work with a database, it's very common to want to know, you know, when was that thing that's in there?
1:15 When was it created? And maybe even who created it would have a created date. I'm not gonna set the value of just yet in the Cree 18 user.
1:26 No, Maybe we should actually set this up with a foreign key relationship over to the users.
1:31 But I kind of want a history of, like, you know, the email or the name of the user that was here. There's this tension.
1:38 If we have this relationship a foreign key, it has to always exist to a real user.
1:42 So what if you delete the user after the page was created and it gets complicated, so I'm not gonna create a relationship just gonna put like, some information so we can track them back down now for the URL is gonna be a sequel.
1:55 Alchemy column as they all are. So I guess I could that for all of them. And this woman is gonna be a sequel. Alchemy String.
2:06 So we're gonna have a string for this one, and actually same or those and this one as well. This one is gonna be a date. Time.
2:14 All right, that's off to a good start. Now we have to think about another couple of options here. Do we want to query the page by the contents?
2:22 No, never. We just want to get the page. And here the contents. We don't want we re by the HTML or anything like that. Same for the title.
2:30 So these don't need indexes, but it's very likely. In fact, the primary way we're going to get a page back is to say they tried to go to this.
2:38 Your l do we have a page for that girl and because we want to make queries by frequently, we're going to set an index here.
2:46 This can make with lots of datas could make it 1000 times faster just by adding that little bit there.
2:51 It makes it slower to write them, but it's very slow that we're creating pages and is very frequent that we're reading them to show users.
2:57 Similarly, probably we don't care about Phil Dream by the user. Maybe we dio Probably not. We can always had that later.
3:03 But this one you're not gonna query by date time, But we might wanna in our admin page show the newest created pages first.
3:11 So over here will have an index. True, because if you order by on index, that's also much faster.
3:18 Now, down here, we can say whether or not these air required, but it's a notable is false be a little bit explicit about that one.
3:26 This one obviously is required with that. Maybe the contents were not required in the beginning. Now, for the date were not putting that.
3:34 Instead, we're gonna put something else.
3:35 We're gonna have a default value, cause, like the i D, which is auto incriminating here means we don't have to set it.
3:41 We just save it without setting that value to get set by the database.
3:44 We want to do the same thing here, so we're gonna set the default to be daytime dot date time dot now and be very careful if I hit enter notice it puts the parentheses here and it calls it.
3:56 That would be basically setting the default to the time that the program started.
4:01 We don't want that what we want us to give it a function the now function so that whenever a new record has entered, secret alchemy will call the now function at the time it was entered.
4:11 Okay, so it's super easy for the editor send you down the wrong path. In that case, well, that looks like we're probably good.
4:18 I think we're ready to go and create our tables here now.
4:22 The final thing to do in order for sequel Alchemy to know about the page object is we have to go over to our all models here and included though super easy, which go over here and we just say import pages now all that does is it.
4:38 Make sure that this file is imported into the process before we go to sequel alchemy and say, create all the tables instead of their relationships and things like that.
4:48 It's possible that we don't have to do this, but under certain circumstances it's definitely required.
4:52 This makes it work for the Web app as well as for the Olymic migration extensions and auto generating migrations.
4:59 And what not So we definitely want to make sure that it's in here at super easy to do. But it's also easy to forget. Just we add something new.
5:06 We added in here, and that sort of derives the rest of the app to know about it. All right, that's it. I think our page class is ready to go.

Talk Python's Mastodon Michael Kennedy's Mastodon