Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The User Model4:57 with Kenneth Love
Nearly every good web app needs a way to represent a user, so we need to create our `User` model. We'll hold onto just a few fields so we can get people in and using our app as easily as possible.
order_by can take one or more fields to order records by by default. Each field can also have a
- at the beginning to mark it as being sorted in descending order.
So why can't we do
order_by = ('joined_at', 'desc')? Because each member of the
order_by tuple is the name of a field, so that would be telling our model to order by the
'joined_at' field and the
'desc' field, which doesn't exist.
order_by is a tuple (you can use a list if you want), we have to include that trailing comma if there's only one tuple member.
It wouldn't do us much good to build this if we didn't have users that could, 0:00 you know, use it. 0:03 Social apps kind of fall apart when there's no one to be social with. 0:04 One way to make it social is to use third party log ins, and let people sign up and 0:08 log in with Twitter or Facebook. 0:11 It's always seemed weird to me, 0:13 to be able to log in to a social site with some other social site's credentials. 0:14 And, that doesn't help us learn how to model these things ourselves, does it? 0:18 That's why I'm going to show you a different way. 0:22 All right. So the first thing we need to do, is, 0:25 we need to make our models.py file. 0:27 So we'll go to File > New File, and we'll call it models.py. 0:30 Now, inside of here, just as we did the databases course, 0:35 we want to import everything from peewee, [SOUND] so from peewee import star. 0:39 And, our user model is gonna be pretty simple, really. 0:45 Let's see. 0:50 So, class user, and it's gonna be a model. 0:51 So we're gonna give them, a username. 0:54 We don't really have to, I mean, most of what we're gonna build, 0:56 we could just use the ID, which is a field that's automatically created and 0:59 it auto-increments, so the first one gets number one, 1:03 the next one gets number two and so on. 1:05 But I like having a username to put into URLs instead of giving them like, 1:07 you're following user number five, but it doesn't really matter. 1:11 It's kinda your choice. 1:14 So we'll say username, which is a CharField, [SOUND] and 1:16 we wanna make sure these are unique. 1:20 We don't want them to be able to have two people with the same username 1:23 cuz we wanna look them up by that username. 1:26 And then we'll do email which is also a CharField, 1:30 not a chair field, so watch out for that typo. 1:33 Again, we wanna make this one unique because we only wanna have 1:38 one account per email address. 1:41 We don't want somebody to be able to have five different accounts with the same 1:42 email address. 1:45 They need to store a password. 1:47 And this is also going to be a CharField. 1:49 And we're gonna set a max length on this one of 100. 1:51 The, hashing library that we use produces hashes of around, like, 1:54 60 characters or so. 1:58 But we want to have some wiggle room just in case something changes. 1:59 Maybe we change hashing algorithms later or whatever. 2:03 And then I wanna hold on to a couple of things. 2:07 I wanna hold on to when they joined, [SOUND] and 2:09 we'll set the default on this to datetime.datetime.now. 2:12 Oops. 2:17 No parenthesis on that, remember we don't want to put a parenthesis or 2:18 else it'll be set to whenever the script runs. 2:22 Not whenever the model's actually created. 2:24 [SOUND] And then we'll hold onto a field called is_admin, 2:27 [SOUND] and this is really more of setting you up for later. 2:32 Oops, sorry. 2:37 That should be false. 2:37 This is really more of setting you up for later to where you're finished with 2:38 this course, you wanna expand this, you wanna go a little farther. 2:43 And maybe, if the user's an admin, they get to see some special UI abilities. 2:46 Or maybe they get a special form where they can talk or whatever. 2:50 Since we're using this datetime right here, 2:54 we are going to want to import datetime, so 2:58 we'll say import datetime, and we should probably go ahead and set up our database. 3:01 I'm gonna use SQLite again, just as we did in the databases course. 3:06 But if you want to use MySQL or Postgres, that's fine as well. 3:11 And since this is going to be just the single database that we're gonna talk 3:15 about in multiple locations, I'm gonna put it in all caps. 3:19 Now in Python, putting it in all caps doesn't make it a constant, but 3:22 it tells people that you're wanting this to be a constant. 3:26 So database and let's just say social.db. 3:29 If you want to use a different name, that's fine. 3:37 It seems like I used just these simple little names. 3:39 And then, let's make this a little bigger. 3:43 There we go. 3:44 And then down at the bottom of course, now that we've made this model, 3:47 we have to set our class meta. 3:50 And we have to say that database is equal to DATABASE, 3:52 all caps, down there at the bottom. 3:55 And then, if we were going to build a view, which we're not going to, but 3:58 you can if you want to. 4:02 Build a view where we list all the users. 4:04 So like, I wanna go find, you know, my friend Jim, or my friend Ashley. 4:06 We're gonna set how we would order them and we're gonna order them by joined at. 4:10 [SOUND] But with a minus sign at the beginning of it. 4:16 And so that tells order by to do this descending. 4:20 So our newest users are going to be sorted at the top. 4:23 So, you'll get to see people who just signed up, and then when you get to 4:26 the bottom, you'll see the people who've been here for a while. 4:30 All right, so I'm gonna save this. 4:34 I'm not actually gonna create the, database file right now. 4:36 I'm not gonna run like initialize or anything. 4:40 We'll do that later on when we have a couple more things built. 4:42 Our user model is nice and simple for now. 4:46 I'm sure that it'll change before we're done with this course. 4:49 In fact, in the next video, we're going to add some functionality to our 4:52 user model so that we'll be able to have users sign in. 4:55
You need to sign up for Treehouse in order to download course files.Sign up