Custom User Manager4:49 with Kenneth Love
Django's models use model managers to interact with the ORM. When you're going to create a custom user, you need to create a custom model manager for it, too.
Now, I need to set a stage here just for a second. 0:00 For the user that I'm going to create, I could definitely take a short cut and 0:04 just extend Django's existing user, or the base user, 0:08 the custom user that Jingo gives you. 0:11 Either of those methods though would be a little bit less informative, and 0:14 a little bit less illustrate. 0:17 Actually, a lot less Illustrated, and a lot less informative for you. 0:18 So just bear with me while I make a model that looks a lot like the existing user 0:22 but will show you how to create a custom user when that need arises for you. 0:26 Before I can create a wholly new user, I need to import Django's class 0:31 that all user models should be based on which is abstract base user. 0:35 So from Django.contrib.models import, and then I need a few things, so 0:40 I'm gonna use parenthesis here. 0:45 So I need abstract base user, but I need a couple of other things too. 0:47 I also need base user manager, 0:52 which is the model manager that all of Django's user models use. 0:54 This one is more abstract ones that like Django uses for 0:59 its actual user are more developed than this one is, but 1:02 this is a good place to start when you're building your own. 1:06 And I also need the permissions Mixon because I want the users 1:09 that I create to have groups permissions all the goodies that that brings. 1:14 So just to reiterate, 1:19 this model is going to end up looking a lot like Django's existing user model. 1:21 Okay so then I'm wanna import models like always. 1:26 And then I'm need some time stuff so 1:30 I wanna say from django.utils import time zone. 1:32 So all right in good shape. 1:37 The first thing I want to create is I'm going to create the model manager. 1:40 So I;m gonna call this user manager and it's going to extend base user manager. 1:43 And then inside here I need two methods. 1:52 I need a create user method and 1:54 I need a create superuser method. 1:58 So let me start with create user. 2:03 That's gonna take self, e-mail, a user name, display name, 2:05 which is going to be none cuz they could choose just not to have one. 2:10 And then password is also going to be none. 2:14 Just for some reason, they don't want to accept the password. 2:18 If they don't send an email though, I'm going to raise a value error 2:22 that says users must have an email address because it's just absolutely required. 2:27 And if they don't have a user or a display name, 2:35 then I'm gonna set the display name equal to the username they've provided. 2:41 So if for some reason you sign up without a display name you'll be okay. 2:48 You'll just use your user name. 2:51 So I'm gonna make a new user. 2:53 So self.model, which is what every model manager gets attached to. 2:55 And, email is going to be equal to self.normalize email. 3:01 And, lets just make sure that all the email addresses throughout your app 3:06 are formatted the same way. 3:09 And, the username is going to be equal to the user name. 3:12 And, the display name is going to be equal to the display name. 3:15 Nothing too weird there. 3:19 So now I have a user instance in memory. 3:22 It hasn't been saved to the database but 3:24 I have a user instance somebody user set password and 3:26 then use the pastor they provided and this is going to handle the encryption of 3:31 the password making sure it goes through validation checks all that kind of stuff. 3:33 I'm going to user.save and then I'm gonna return the user. 3:39 All right. So that's pretty straightforward. 3:42 So now inside create superuser, 3:45 I'm probably doing something pretty similar, right? 3:47 And I am. I'm gonna take the user name or the email, 3:50 the user name, the display name, and the password. 3:52 They have to have, superusers have to have everything. 3:56 That's that's my that's my rule. 4:01 So I'm going to make a new user, and I'm going to call self.createuser. 4:03 And I'm going to pass in the email, the user name, the display name, 4:09 and the password. 4:13 And then I'm gonna say that user is staff because they are, 4:16 they should they should be able to login to the admin and they are a super user. 4:24 Set both of those things to true and then I'm gonna call user.save and 4:29 I'm gonna return the user. 4:34 So two methods and these two methods just handle creating users. 4:35 This is mainly going to be done through the command line right when you call 4:40 Python managed up high create super user. 4:44 This is what it's going to call. 4:47
You need to sign up for Treehouse in order to download course files.Sign up