#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Define your database model

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we have a very basic app. Now, as this web app is going to allow users to create user accounts on our website and then recall them
0:11 for the sake of being able to log in, we need a database. That's the next thing we're going to set up.
0:17 And for that we're going to use Flask-SQLAlchemy. Now SQLAlchemy uses the models system that is to say we create a model class
0:28 that defines what our data structure is going to look like it defines what our database is going to look like. So lets create a models.py file
0:38 and I'll show you what this looks like. First we'll begin by importing Flask SQLAlchemy so from flask_sqlalchemy import SQLAlchemy
0:52 And next we want to actually define our class. So there are other things to import here which we'll get to.
1:01 But first let's just stick with the SQLAlchemy stuff. So we want to create our user class because this is the data set that we're playing with
1:09 we're playing with user data. So our user is going to actually require a database model and we'll do that by importing db.model.
1:20 Now obviously I haven't actually imported any db or we haven't made reference to db yet and we'll get to that in just a sec.
1:27 But we need to do this first so, user, db model. Next, we're going to actually create an id for our data so every time someone creates a user account
1:39 they're going to get a unique id. And this is, you know, standard across most databases that you're going to create in the future.
1:47 This also allows us to have users with the same names. So obviously there's not just one Julian one Mike, one Bob in the world.
1:55 There are many of us out there so we need to be able to account for multiple users with the same name and this is what helps.
2:02 So id, and now we define what this column in the database is going to contain, what it's going to look like.
2:11 So, our database column is going to contain an integer, so a number, and that as we know is the id. So your id's are just generally four numbers
2:22 you don't have an id of 1.753 it's an id of one, two, three, and so on. And this is going to be the primary key of our data.
2:33 So, primary key equals true, and that's that for id. What else do we need our user class to have this database model class to have?
2:43 It needs to have a username so when you sign in and log in to the website you're going to a username and you'll need a password.
2:50 So these two are going to be quite similar so we're going to need a username and it's going to database column again
2:57 and how are we going to define this column? It's going to be, the data will be defined as a string because the username is a string.
3:08 And does it have to be unique? No. So we'll say false. This 80 here, is how many characters that string can be so we're going to allow them
3:20 to enter up to 80 characters, okay? Next, nullable. Now we're entering this in here because we don't want people to be able to enter nothing.
3:31 It actually has to have some data there. And for our password we're going to do almost the exact same thing.
3:39 So db.string and it's going to be 120 characters this time. And if you're asking why, that's just arbitrary for this use.
3:50 I'm just making those numbers up. So nullable equals false as well we don't want people to have a blank password.
3:58 And unique is false as well because, you know, who knows? Maybe there's a chance someone will have the same password as someone else.
4:06 And finally we want to be able to define a method inside our database model class. And we're going to call on repr, R-E-P-R
4:16 which is just a dunder method for representation. Okay this is what a str, S-T-R dunder method actually calls in the background.
4:26 It's just a way of presenting raw data as it is. And we're going to get it to return user self.username, okay?
4:41 And all that does is, when you call this user when you try to call this user class when you try to return the data, it's going to return it
4:50 in the format of user and then the username. Alright, that's just one little method we can call on this.
4:57 We could obviously create more, but we're keeping it simple and that is our models.py file. One last thing here, is we need to actually get
5:07 the database that we're going to be referencing here. So where does this db.model. come from? Well, we'll create the actual database object
5:16 in the next video, but it will be imported from our Project Awesome App. And we get from that Project Awesome App we're going to import db.
5:29 And you'll see where we import that in the next video. It's going to be from our _init_.py file. And just one last thing, I've realized I have actually
5:39 put through the unique flag for the username as false. That's actually supposed to be true. We want our usernames to be unique, that way when we
5:48 search the database we only land on one. Because if you have two that are the same our query to the database isn't going
5:56 to know which username to pull. So let's delete that now and quickly change that to true. And the last typo I have here is just
6:08 this little apostrophe here. We want to move that down there so user, and then your double brackets and then format, self, username.
6:17 That' it. Let's move on to the next video.


Talk Python's Mastodon Michael Kennedy's Mastodon