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