Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Custom User Model12:24 with Kenneth Love
Now that the custom user model is in place, it's time to create the custom user!
So now I need to actually create the user. 0:00 So the user is going to extend AbstractBaseUser and PermissionsMixin. 0:04 Now you're probably thinking, hey Kenneth, you just put PermissionsMixin, 0:12 second mixin should come first. 0:15 I'm following the docs on this one. 0:17 All of the examples in the docs show the PermissionsMixin coming second, so 0:19 that's the way I'm gonna do it. 0:22 So now I'm gonna define what the what the user looks like. 0:24 So the user is gonna have an email field, models.EmailField, 0:26 and this one and has to be unique, right? 0:32 One account per email address. 0:35 And then username is gonna be a CharField. 0:38 And I want this to only be 40 characters at most, and I also want it to be unique. 0:44 So then we're gonna have a display name, which is also gonna be a CharField. 0:51 This one though, the length can be a lot longer we'll say 140. 0:56 And I know I said before if they leave it blank 1:01 the model says they can't leave it blank but just whatever, if something happens. 1:04 These don't have to be unique these kind of be basically whatever. 1:08 So a bio, so I'm sending this is a CharField so 1:13 that I can control the length, I don't want people entering huge ridiculous bios. 1:18 But I don't really care what they put in here and 1:23 it's gonna default to an empty string. 1:25 Avatar is gonna be an image field. 1:28 And blank equals true, and null equals true, and I have to tell you right now I'm 1:32 not actually gonna show how to do image uploads. 1:36 That's a really great thing for you to look through the documentation for. 1:39 It's not that hard to do and the docs will teach you. 1:41 So now I'm gonna hold on to date joined. 1:45 Which is gonna be a date time field. 1:49 And the default is gonna be equal to timezone.now. 1:52 So that way whenever they sign up, I know exactly when they signed up. 1:57 is_active is going to be a Boolean and the default equals true. 2:00 And is_staff is going to be Boolean as well, and 2:10 the default on that is gonna be false. 2:15 Now the is_active provides a really nice way of turning a user off. 2:17 So if somebody is like, I wanna cancel my account, I wanna get out of here. 2:22 As bad as it is to hold on to data for people who want to leave your service. 2:25 Although a lot of times you'll have things connected to their user model. 2:29 You can't delete those things and you can't delete the user model because they 2:33 need to have data that doesn't connect to something. 2:36 So you can just mark them as no longer being active, they can't log in, 2:38 nothing else whatever. 2:41 You can hide posts that are from an active users, whatever. 2:43 And your database still stays good. 2:46 So I'm gonna set a new item here. 2:49 And this is going to be an attribute, this isn't actually a field. 2:52 And it's called objects, and it points to UserManager. 2:55 And this is just like when you normally would do user.objects.all. 2:59 Objects are going to the manager. 3:04 This is always happening with Django apps, or Django models. 3:06 So no need to add two more attributes and these are both in all caps. 3:10 So the first one is a USERNAME_FIELD and this is what field are we using as 3:13 the identifier, the unique identifier to look people up in the database. 3:18 We actually have two, we have email and we have username. 3:23 Either one of those would work, but I want to log in with the email addresses, so 3:26 I'm gonna say email. 3:29 And then REQUIRED_FIELDS is a list. 3:31 And this is a list of fields that are sent 3:35 through to the create superuser that are outside of the username and the password. 3:38 So and username being the username field which in this case is email not username, 3:43 just remember that. 3:48 So I wanna send display name in and i'm gonna send user name in. 3:49 Okay, so now i'm gonna add just a couple of methods here. 3:55 So for string I'm gonna return 3:59 @.format self.username. 4:04 I'm just kind of copying slack toward so many other places, right? 4:09 And then two methods that Django uses quite often is short name and full. 4:15 And these are just ways to refer to somebody using a short name or 4:21 a long name. 4:26 And you can override them with these methods. 4:28 And so here I'm gonna return that, 4:33 add that format self.displayname, self.username. 4:37 Yeah, that'll work. 4:45 Okay, so just to recap, the UserManager is what's gonna be 4:46 used to create new users and superusers, all that kind of stuff. 4:51 The methods are the ones that get called to create those users. 4:55 I mistyped something up here. 5:00 Default and then right. 5:04 So those are used to create users. 5:07 And then we have our user model which has email username display name bio and avatar 5:08 date joint is active in is staff all of which are normal everyday model fields. 5:13 The objects attribute which specifies which manager to use for 5:17 looking the stuff up. 5:21 And then of course the user name field in and 5:24 the required fields which are attributes we added to specify how this user is used. 5:26 So how does Django uses this user to look it up, 5:32 to refer to it from other ones, to logon with it, to do all of that stuff? 5:35 Then next thing that I need because I just created a new model is 5:40 to run migrations, right? 5:44 All right, manage.py makemigrations accounts. 5:47 Now but before I do that actually, I need to come over here to settings. 5:55 Let's do this next bit. 6:01 And down here at the bottom I'm actually gonna add a new thing 6:03 AUTH_USER_MODEL which is gonna be able to accounts.User. 6:08 So now if I make the migration During those complaining about other stuff. 6:13 Because we have things that point to somewhere else. 6:22 This is the fun thing with doing these. 6:24 So, our migration will come. 6:26 I will do the migration. 6:27 For now though, I need to go change these other models. 6:29 All right, so I've changed this AUTH_USER_MODEL. 6:31 This is a setting telling Django what model to use as the user model for 6:34 this project. 6:40 Okay, so to make use of that I'm actually gonna go here into communities models.py. 6:42 And up here at the top I'm gonna say from django.settings import or 6:48 sorry, not django.settings, django.conf import settings. 6:55 And then I don't need this user import anymore. 7:01 So I'm gonna come down here and everywhere that I was linking to 7:03 user I'm now gonna do settings.AUTH_USER_MODEL. 7:08 And that made that line a little long, so I'll do that. 7:13 And down here, I have user. 7:17 So this also becomes settings.AUTH_USER_MODEL. 7:19 And I'll bump that down to the next line. 7:23 Okay, so that's all of that stuff. 7:30 And so now I need to do that inside of posts as well. 7:34 So posts > models and 7:39 instead of importing user from django.comf import settings. 7:44 And instead of the Post being related to the user, 7:49 the Post is related to settings.AUTH_USER_MODEL. 7:52 And that's all I need to change in that one. 7:59 All right, so I should be able to make migrations at this point. 8:01 So let's try it, python manage.py makemigrations 8:05 accounts, or makemigration, yeah. 8:10 Okay, and I show do python manage.py migrate accounts. 8:17 And yes I'm fine with the leading the AUTH_USER_ACCOUNT. 8:24 And then python manage.py makemigrations community. 8:29 Or actually sorry, that should be communities. 8:36 And probably nothing for posts either. 8:40 Nope, okay, so I just needed to migrate accounts. 8:43 So now I can run my server again. 8:49 And there is another change that I need to make. 8:52 So I changed all the places the models are related but if you remember, and 8:56 it's me testing how much you remember about what we've done so far inside 9:02 of the forms.py for accounts. 9:07 I created this UserCreate form, and the model it points to is user. 9:12 So it's pointing to the Django user which is not who I wanna point to. 9:16 So I can probably just do the conf settings things, right? 9:21 I can't, because this requires an actual model and 9:26 not a string which is what the others are getting. 9:31 So instead of this, what I'm gonna actually do is from django.contrib.auth, 9:34 I'm gonna import get_user_model. 9:40 And I have, I think that should come first. 9:45 Okay, so get_user+model is a function that will go out find 9:48 the actual class that is the user model and give me that back. 9:52 So instead of user, I'm gonna call get_user_model. 9:58 Safe, done, that's it. 10:02 So now I can go, I have to actually put the word import in there. 10:05 There we go. 10:12 All right, run that. 10:12 Okay, so now I should be able to, 10:16 if I'm over here I'm probably gonna get that I'm not logged in, right? 10:20 Cuz I'm not and I don't even have an account. 10:23 So if I go to accounts, sign up. 10:27 Actually I want this to be a superuser. 10:33 So let me test the superuser as well. 10:37 So python manage.py createsuperuser. 10:40 So it asked me for my email address so that's firstname.lastname@example.org. 10:46 The display name that I want is __<3__. 10:52 The username I want is kennethlove, and 10:57 the password will be testpassword. 11:01 Create_superuser got an unexpected keyword argument password. 11:05 Let's go see what we did wrong. 11:10 Should be two S's in that. 11:16 All right, so let's run this again. 11:20 And I've created my superuser. 11:31 So now I should be able to run the server and 11:33 I should be able to sign in with the account that I just made. 11:36 So let me go to login, 11:42 And it's email@example.com. 11:48 Testpassword and I have successfully logged in. 11:53 So if you want to be able to create and 11:58 edit these custom users in the admin you're going to need to add some 11:59 admin configuration, check the teacher's notes for links to documentation for that. 12:02 Well, there are a lot of pieces to using a custom user. 12:08 None of them are extremely difficult and now that it's set up you shouldn't have to 12:12 touch most of this again even if you need to migrate your model. 12:15 A well made third party app will use Django's get user model function, 12:17 too so they'll work with the users as well. 12:22
You need to sign up for Treehouse in order to download course files.Sign up