Custom Django User Model // DJANGO Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Yeah 'Graceful'

👍︎︎ 2 👤︎︎ u/Crealone 📅︎︎ Oct 10 2017 🗫︎ replies
Captions
hey youtubers it's Justin and this one we're gonna be looking into how we can change the default Django user model to our own custom user model one of the biggest reasons to do this is to change that user name field to being required to not even being there to making the email field the required way to log in so that's the main purpose behind this now I will say that this is a another portion of the e-commerce course that we have that is at join CFE comm slash courses slash ecommerce there you'll see a lot more to it than just this piece but this is a self contained piece so you can you can see a lot here and actually get something from this so that's where we'll jump right into now Before we jump in I just wanted to let you know we do have a guy that we're going to be working off for a part of this section the guide is right here and it will help you understand a little bit more about the code that's written through this but it's also a good reference for down the line if you ever need to create a custom Django user model so check out that guide we will be working through it so stick with us but before I start actually doing the coding let's actually look as to why we might want to change how the built in Django user model will be versus what we have so I'm in the Django admin right now of the e-commerce project and I'm going to go into my users here and of course this is activated by default every time you create a brand new Django project this will activate inside of our settings which we can see on Django contrib off and there's a lot more things involved to a user model that don't exist in a standard model that you build yourself right so like in the standard models like like guest emails or cards or products those are different just slightly different than the Django user model because the user model itself handles authentication for us so it handles are like logged in session those sorts of things are rather important now from a lot of apps you might actually have a user name when you use it right so when you're building a web app you might want to use her name Twitter is a really good example of that like there are user names for Twitter users right and that's something that was built into Django by default years ago now in our case we're building an e-commerce site so we don't need a username at all in fact it shouldn't even be the mode of authentication anyway we want an our email address to not only be unique as in one email address for the entire site for that particular email right so one account one email that's what we want just like there's one you name one username across the entire site username is unique we want that same thing for our email but more importantly I want to make email the central focus not user names so that's what we're gonna have to do here is we're gonna create a custom user model specifically for that reason we want to transition away from the username stuff to having just email and actually completely get rid of the username field altogether so let's go ahead and actually jump in and get started again if you're falling along with this guide you will be going into step two in just a moment since the Django user model the built-in user model is activated by default we have to actually treat it differently than any other model that we might be building you know so such as if you go into like an app called products we have our product model and that is built this way there's there are things that we can add to it such as a our own custom model manager but we're not really customizing this model like it's going off of a lot of the defaults that Django has and some things that it might expect such as the STR method but beyond that it doesn't expect a whole lot so our user model our custom user model expects more than the standard model style model and that's because by default the things that it works with like the authentication stuff like logging in our user those sorts of things are required for our user model so what that means is we just can't actually have it as model stock model that's a long way of saying you know that's how you have to do it so we're gonna be putting this model our custom user model inside of our accounts app of course if you've been following along with us you'll know that we already have a model in there and this model has nothing to do with the actual user itself like this is just collecting a guest email and that's it it's not really about authentication where our custom user model is all about authentication we definitely want to make sure that this is done correctly now the first thing I'm going to do is actually import the model we're gonna be in extending from our inheriting from and that's from django that contrib got off that models import and we're gonna import the abstract base user so the abstract base user is the class that we are you are going to be implementing for our user models so we'll say class user and let's just write custom user for for just a moment and I'll I'll go ahead and put base user model here and just say pass so this user model this is actually a user model itself so if I just said pass and tried to run my migrations let's just take a look at what happens so python managed up to I make migrations it actually created it for us right so if we go on our migrations there are fields that come in here these are the default fields to that abstract base user the ID password and last login so that means that I don't actually have to set those fields at all they come in by default which is pretty cool right so I did make migrations which means that I actually don't want this custom user model instead I want to call it user so I'll save that and I'll make my regressions again and we just renamed it to user okay not a big deal so we now have this user class and of course again this is my custom user class not to get confused with the Django user class the reason I call it user is because that's what it is that's how I want to describe it I don't want to describe it as my user or custom user because in the long run this is the user class now it's not the Django user class but it is our user class so that's what I'm gonna call it user now the genuine documentation says my user it's just a little bit different but my philosophy is this hey I'm completely getting rid of the standard Django user class so I'm going to call mine user there's a possibility that in the future that this won't even be allowed but I'm gonna say that it will be because it is something that should be up to the developers like ourselves okay so what are the fields that I want to add to this user class well first of all I want to have an email field here so models dot a male field and actually before I jump into that let's look back at what this user model creates in the migrations itself ID password last login there is literally nothing in here that is showing us that this has user name of any kind right so it's these are just the three defaults ID as a default for any model but then password and last law login are the only things that are actually related to this user so now I have an email field and that's what I want to have right that's what I'll leave it as and I'll go ahead and say unique equals to true now like any field I can actually add a max length in here and I'll do it based off of the documentation and say 255 this is now my user name field so I can actually declare it as such so user name field equals to email what this means is it's no longer that old username field right so I could literally explicitly write out a username field here and do models dot char field and you know fill out that and call that user name so what that means is then I can have some of my own custom user name requirements if I had them I could put them right here too so that would be a way to change that field in particular and of course we're not doing that we want to go off with the email but I just want to give you that a little bit of extra context now from here there might be a few things that we want to add in so whether or not this user is active right so is this user an active user and I'm gonna just say default being true now what I say active I mean can login that's it right login in general the next thing might be staff so models dot boolean field and staff are sorry default being false and then finally we might want to have an admin user so like a super user basically and again the default being false so super user like little literal staff user non super user so of course super user is the one that has control over everything so is the admin of our site ok so now we've got this that's go ahead and make those migrations again and we're getting a non knowable field email without a default so I'm gonna go ahead and just write a default in right now provide a default now and I'll just say ABC the string of abc123 at gmail.com this is we haven't actually added this into our database yet so that default actually doesn't really matter for us now cuz again we haven't added it to our deep database and we don't have any fields in our database so all of that default did was every existing value in the database would have been this but there are none so we're good okay so the next thing is the fields that are required so what fields are we going to require when this user is being created now in our case we have an email field but email and password are required by default so again that password field is already in their email is our username field so really it's the username field and password are required by default so if I added an extra field required fields let's make sure that this is capitalized and this is an array so we already have default values for here let's say for instance I wanted to put full name in here and I did models dot char field max length of 255 and I could say blank equals to true and null equals to true and I can say that that is a required field right you could 100% do that I'm not gonna do that I will leave that field in there but this is where when you're creating things you want to add in these required fields it's a little bit different than a model a standard model this is the user name related stuff and this field will happen or it'll come up while you do something like Python managed to py create super user you know those kinds of calls will go off of these required fields at too okay so we've were really close to being done with our abstract base user but there's a couple other methods that I'm going to include by default one of them is get full name when you return something for that the other one is get short name also need to return something and then of course like any other model we we we want the STR method and we're gonna return something there and then the last couple things is I'm going to add in some new properties so property and we will call it the first one is staff takes itself and of course return self let's staff so is this a staff member and we're gonna do that for a few more so is admin self that admin and then is active so that active okay so these additional properties are just methods that you know if I remember to say dot admin or is admin these will give me the same result and then finally what are these do well get full name would actually come off of the full name field in this case right so we have it in here but it's blank but if you look at that guide that's not even in there so we'll leave it out and return this as self that email all the way through again this is like if you wanted to have these showing up and identifying these users in some fashion that's other than their quote-unquote user name field or the default user name field okay let's go ahead and save it and we'll run our migrations nothing to do and then I'll do Python minutes up py migrate this actually creates that user account in there so we're now ready for the next step which is actually creating the user model manager but what I would say is now that we have this user model we want to actually make sure that this is as best as it's going to be for now because this is not exactly the end-all-be-all for every information for your user all right so there's a there's a possibility that you might actually have a new class called profile and it's a normal model class and really it just takes in a foreign key so models dot one-to-one field of the user and then here you can actually extend you know extra data whatever extra data you need for that specific user so all of that doesn't have to be on the user model itself in my case I want to make sure that yeah my email is on the user model and that's it so even full name even you know like location or all that stuff you might actually create a separate app altogether for handling their profiles like right now I'm have it in the accounts app but I might even have a profile set altogether but that's just one thing I wanted to mention while you're thinking about designing this because designing your user model or your custom user model when you go to change it there there's a lot of unexpected consequences that happen specifically with the user model that just don't happen with other models so I like to keep it nice and simple the the one edition I might have is like full name whether or not they perhaps you would have something like they've activated like they confirmed email so confirmed email or just confirmed in general and that might be another thing that we have or this would be a confirmed date I will I will leave these out but I want to at least show you this so confirm date and that might be a date time field you might even have created date or a timestamp actually let's go ahead and put in a timestamp in here date time field and we'll say Auto now and equals to true okay so so this is where when you finish off your user model you should look at it as like AIDS I'm not going back to change it if I want to extend things to it I use that profile model like I just mentioned so that's it for the first part of creating this custom user model next few things will actually start making this even better stay with us so now it's time to add the user model manager now we're gonna do this prior to wrapping this into our django user model like we haven't changed the default user model yet just by adding this class in so we want to create a model manager first before we even do that so we'll say call it base user manager we have to import that so again it's it's inheriting from some defaults that Django has for us in particular for the user so I'm going to create the base user model or manager excuse me and we'll say user manager equals to base user manager and again we can say pass here and then come down and objects equal to that user manager right so going back up we want to create a few methods here the very first method is create user and this is going to take in self email and password so this is taken in all of the required fields here we did mention that if you did full name you would actually put full name in here as an argument if it was a required field but since it's not a required field we're gonna leave it out doesn't really matter and also we'll say if not email we want to raise a value error or a validation error some sort of error going on will just raise a value error because this is what the documentation says and we'll just say users must have an email address so if the user if not user email or if not the email being passed into this create user model then we will raise that otherwise we'll just say user equals to self up model and just create it and a easy way to do this is email equals to self dot normalize email of the email this is a built in method for that base user manager it's just going to normalize our email for us so we can use it over and over again and then after that we'll do user dot set password this is how you set a user password you don't write user dot password equals to password you use set password equals to password now that's also how you would change the password to and then user dot save and we're going to be using self dot underscore DB and then finally we return that user object so this is really just the user obj and or user instance that's coming back so that's how you create a user not really that complicated right so I can actually add in a few things in here too and I could say is staff equals to false is admin equals to false so then I can use these as additional parameters that I could pass in here so such as user object user obj dot staff equals to is staff do the same thing for the admin and yeah you could even do the same thing for active so active is true and then user object active equals to active or well let's stick with the same syntax and we'll say is active so this is going off of all the default so what that means is then down here we'll say define create staff user and it's going to take in self email password keep going to none and same thing is we're going to go ahead and go ahead and say self our user equals to self dot create user and we'll say email of course and then password equals the password and then is staff equals to true and then will return that user really simple oh one other thing I will say is if we don't have a password we'll say if not password in here too will also raise a value error and say password our users must have a password okay and then from there let's we've got our staff user now let's go ahead and create our super user and is staff is true and we're gonna call this create super user we also want to say is admin equals to true so it's going off of our new create class and we have all of these different arguments that we're passing through so we can use them down here so now we actually have in our model manager really cool pretty easy to do not that big of a deal but it's we want to create these three methods to make sure that when we're creating our user it goes off of our new requirements now the other one like if you have the user name you would see user name and then email and password and so on alright so any required field you would just add to that and then also the sub but the other you know sort of methods that you'd be using these sub methods for other kinds of users right you would add in any required field into it so then these are called when we run various built-in methods for Django but before we actually again before we actually move this over to our standard user model or replacing the standard user model we want to actually create some forms so our admin is set up to handle this correctly so stay with us so now that we have our brand new user and our user manager we can now change our off user model or the built-in user model to this new one so let's go into our settings and we're gonna go ahead and just underneath installed apps we're gonna put off underscore user underscore model and we're gonna put it equal to accounts that user so this changes the built-in user model to ours and before I actually save those settings I'm gonna comment that out and I want to make sure that I'm running Python manage top py make migrations and then I actually have this thing of timestamp is not on there so I'm gonna go ahead and provide a one-off default and do timezone dot now and then python manage up py migrate now the reason I did this is because I want to make sure that that model itself is in the database it's already there so when I switch over I'm not gonna run into any database driven errors I mean I still might so let's go ahead and see what happens so I'm gonna I'm gonna list it out let's go ahead and change it over and I'll do pipe and save it of course and then now python managed to py make migrations python managed up py migrate oops spell migrate correctly and now that should have saved it let's try it again it doesn't appear that anything changed so let's go ahead and look at our admin itself let's run the server again and we run the server now it's saying email so it there's a good chance that we don't actually have any users right now so I'll do Python managed uh py create super user and email will do hello at team CFE comm whatever password whatever password and we get this unique constraint failed so there is something in our billing profile let's go ahead and look at our billing profile down in here we've got this create stuff right so it's showing me that it's trying to create from the user and it's giving me a unique constraint failed well that should make sense because I already have a billing profile of that user so this is where okay now we're in this like hey I just changed my user model and it's causing all sorts of issues with my system so this is because of the different the various signals that we already have now there's two ways to go about solving this now since we're actually working with like just test data I can actually just change my database or delete my database altogether but before do that I want to save some things inside of my database more specifically I want to make sure my products are backed up everything else doesn't really matter everything else has been like just test data but my products like also are just test data but I want to like actually have them saved right so let's go ahead and stop for just a moment and then come back and do some things related to fixtures and just changing over our database and just basically starting fresh so stick with us what I'm about to show you is something that's really useful when it comes to bringing in something brand-new that might create a lot of problems in our database and that is this concept of fixtures what fixtures allow us to do is to kind of save our pre-existing data from our database clean out that database and then refresh it with some new models or new app or something like that so I'm gonna be backing up essentially backing up my products model and that's it like that's the only one I want to back up so everything that's in here is gonna be saved for us and I'm gonna save it in a folder inside of the products app called fixtures now this concept is not just for Jengo user models at all but since we ran into that issue where when the last time when we tried to create a new user I get this unique constraint failed now granted I could go in there and delete all of those building profiles but instead this is a great opportunity to see how we can delete our entire database but still keep part of it so we can work with that part of it and then more or less start with a fresh database and fresh install this also means that I'm going to drop all of my migrations related to my accounts app so we went through these migrations as to just to learn but now we're gonna go ahead and delete them in just a moment so to create a fixture we go ahead and run Python manage to py and we call dump data so dump data will make a fixture for us and I can give it a format of JSON and we can indent it however we'd like so I'm an indented four I hit enter and it gives it it actually gives me everything I see admin in here I scroll up a lot a lot I see orders I see all of this stuff this is this is cool so it's actually saving some data for me and then I see all this text here and what is it what is this text related to it's related to products so my dump data is can be refined I can say the app that I want I can even say the app and the model right so the app name and then even the model name to get really specific I can hit enter and it'll give me that model of course I can if I just did products anyway it's gonna give me all of the apps in sorry excuse me all of the models inside that app which in this case we only have one model in there and those are the fixtures themselves we can actually copy and paste all of that data and save it into a file ourselves' but we just created a fixture inside of the products app so if we go into products we see that there's this folder called fixtures so what I'm going to do is on a do this call and then I'm just gonna go to products / fixtures / and there's gonna be products that JSON the format is JSON so we can save it as JSON data the intent is for so it's easier to read you know go ahead and copy this and I'm gonna just save it inside of that notes thing that we did and I'll keep it in this checkout process and the same backup fixtures just like that just so you guys have a reference for it and now if I go into that products app I see products JSON okay cool so I'm going to rename my database to DB DB 2 dot single light and now I have a clean database it's completely empty or I have the opportunity to create a clean one so I'm also gonna get rid of all of my migrations inside of this app so inside of my accounts app not everywhere and you could do it everywhere but in my case I'm only gonna do it in accounts because that's mostly the reason why we're doing this in the first place so I'm gonna delete all these I'm also going to delete the cache and just keep that init file in there okay so now what have we've done we've backed up our products in the ways in a way of fixtures which we can look at those products these are all of them it has the the price the product image everything that I would need to read it once I deleted that I changed my database so then our current database has to be changed or recreated and then I have to actually re implement my accounts so I can do something like this so let's leave it as is and see what happens I'm gonna go ahead and run Python managed up you I make migrations notice it says create user model and create guest email now if I look at my initial look at my user model it has all of those things that we already put in there right including password and last login very good and now I can run Python Manish that py migrate it runs all of the migrations as expected again I didn't delete any of these other migrations I just still just deleted some of the data that was already in the database so this cleared out my database and now all I need to do is run Python manage top UI load data products flush fixtures select products JSON this loads in that data and now I can run again Python managed up UI start excuse me create super user and hello at team CFE comm a password another password super user created successfully now we can run our server again and we can use that email and that password and now I've got user has no attribute has model perms so this is something that we might have to actually update on our user model but the point here is that we didn't receive any errors and we were able to refresh our database using fixtures so we'll fix this still so stay with us but that main part of using fixtures can be used all the time and you can do it as a way to backup some of this data without having to backup the entire database that's something else that's really cool about pictures themselves so if you want to know more about fixtures let us know in the comments below otherwise let's keep going now it's time to create forms and then eventually work in the admin to make some changes but before I do that I I did leave off where I had this error of attribute error user has no attribute has model perms so that's where some of these defaults like get full name and get short name comment right in our case our model we'd left down a couple and that is defined has perm takes in self perm as in permission object is none and then we're just going to return true so these are model level permissions I'm actually not going to be using the built-in Django models permissions so model perms and this is self app label and this is coming directly from their documentation as well as our guide and then you just return true here so now that I've got that that should get rid of that error and it does and now that we're in the admin we see hey where's my user model it is gone and it's also not in accounts so we can change that right I'm gonna jump into our admin here and I'm gonna go ahead and import my user model now I can do it from directly from models itself or I can try out from Django contrib got off import get user model and say user equals to get user model and then do admin dot site dot register user so we'll save that refresh in here and now I've got my user here I click on it I click on any given user this is what I see it's definitely a lot different than my old user model right or the way I could see my old admin and then when I add another one this is also what I see this this is not great for what we've got here so what we want to do is create some custom fields or excuse me custom forms that will handle this for me now in the Django admin if you're not that familiar with it we can actually do stuff like class let's say user admin and we do admin and Donna model admin and we can say class meta and I'm just doing a really simple example here of this and we'll say user and let's say for instance like I don't know search search fields I think it was or searchable fields let's try oh no search fields that's definitely what it is and we'll just put in email so I say that I go back in here refresh oops we want to put this also in register it together and I can refresh in here and now I can actually do a search by my users right so hello at and it will actually do a search for me it'll find of course the ones that we have that's useful in fact I won't keep it for my user model I'll keep it for my guest user model or guest email admin all that stuff but instead I want to actually change this and now before I can change this I want to implement a new form that will actually do it so instead of forms top pipe I'm actually going to be going directly off the guide just scroll down a little bit and look for the bottom portion of this and we're gonna paste this in here so we've got our user admin creation form and our user admin change form so we've got two forms in here now that I want to update and I need to do one more import and that is this right here this is a this is that read-only password hash field we'll see what that is in just a moment so these forms hopefully you already have an idea of how to use these forms but the main ones are being this admin creation form when we go in to save we're going off of the clean data that's passed we're not entering in raw emails or excuse me raw password fields at all and it's coming through and saving and then everything else is just based off of our custom user model so if you wanted more required fields in here than just password and email this is where you could do it right so like we talked about full-name as an example you could put that in there as well and just write whatever fields you need to be required and then that admin creation form will do that same is true about the admin change form and let's go ahead and see what these things look like actually inside of our admin so I'm going to go ahead and import them and we'll do from dot forms import atom in creation form and admin change form okay so I'm going to adjust my user admin a little bit and comment out this class meta so this meta class and just say form equals to user admin change form and then add form equals to user admin creation form so this is the edit view or the update view basically and this is the create view as far as crud is concerned so now that we've got this let's go ahead and change or take a look at that admin again this list view looks the same but when I go to the Edit view here's the new view right it changed slightly or well more than slightly right it actually is in here as my email my password and all that does look a lot different if I go ahead and come back and add another user now I've got something like this right so it's ever so slightly different now I want this to be a little bit more robust but it is important to note that yeah I'm using model admin here versus what we're about to use and that's also in that guide which we're just going to copy again and just talk about because we really didn't want to go too much into detail about the admin and the forms and whatnot in there so I'm gonna go ahead and import some stuff from there and we'll come in and just change our user admin so this whole thing right here I'm just changing it to some of these defaults I've got my registered admin and then finally I want to remove the group because we're not using it we're not using that group model anymore okay so let's go line by line first two things we've already seen these list displaying list filter are gonna give us you know just better overview of our stuff on the ListView and oops I had it registered twice so refresh that so our list view changed a little bit right so we've got a filter here now we've got admin and it's currently by admin that's it so well we could also do staff active and so on right so staff inactive I can filter these things down however I see fit well let's bring it back to being all and then I can go on here and I've got the unknown field sets here so this personal info I don't actually have any field sets in there or fields in there so let's remove that's that empty tuple or the empty string for the temple and there we go we've got our email our password and whether or not we've got the permissions of admin and again more permissions we can have our staff active and so on so we can really play around with it there on what fields that we want to have personal info well that has to do with like if we were going to use that full name field okay let's bring that out that's where you'd be able to do it and then adding a user is now this field or this form where it has our email and our password so if I set ABC at gmail.com whatever password and hit save oops I got user creation form not defined I think I know why because if we go back into our forms here yours will not say this because I'm gonna update the guide but basically I ran the wrong superclass there try that again and there we go so we now have two users that we can work with okay so what we are going to do is we're gonna challenge you or leave you with a challenge to how do you actually add a required field to your model and how would you actually like add in a non required field or some personal information in here as well so that's what I'm gonna leave you with on this one if you have any other related questions to this let us know and again this guide is going to be really really useful for you in building out what we just did thanks for watching we'll see you very soon so now let's go back and actually create a required field that we might add to this model so our user model we're gonna just go off and do what we've already been talking about which is this full name field first of all I will make it a required field in here I will leave all of the other ones all the previously created ones as full name or I'll leave them in there so here is our full name right so I'm adding in this field I'm adding it as a required field as well so that means that in my create user I have to actually have a full name field in here and again I'll say if not full name raised value error users must have a full name and again you full name all throughout our various calls okay so I've updated my model stop hi I'm gonna go ahead and run Python manage top py make migrations and then Python manage top py migrate cool so I've added my full name to the user and it is now considered a required field so what that means is then if I came in here and did Python manage top py create super user I'm gonna get email I'll say abc123 at gmail because I know that one doesn't exist now I get full name Justin Mitchell that's my name and then a password and a password cool so that is a required field now of course now what we need to do is in our forms our creation form we have to put full name in here and then in our change form we also want to put full name in here not a huge deal and then an admin dot PI personal infill info we're gonna add in full name ok put a comma at the end there and that should do it let's go back into our admin let's click on any given user abc123 a gmail did not save my full name user so let's see why in our models we have well we don't actually set it anywhere so let's put it in here so full name equals to full name good so let's try and create another one and this time ABC 1 2 @ gmail.com again full name created successfully I still have the server running let's go back in oops 1 2 and what do you know there's our server there's my full name looking good so that's that's a required field right so like if I tried to run create super user again and I'll just give it whatever email and try and do it now passwords aren't our blank passwords aren't allowed so let's try that one gives me a value error right users must have a full name so it's actually preventing me from creating that user like if i refresh in here the user was not created ok rather important there but I don't actually want full name to be required I just want to show you an example so I will reverse everything I just did in the sense that I'm going to get rid of it or well I can get rid of the value error I can put full name in here as being none also in here as being none and also in here as being none oops not none none day but none so we we still have the ability to create the user with the full name but I just I don't actually need to set it anymore so it's no longer required so we'll just come down here and just get rid of that required field and I will have it in here and then finally get full name we'll just say if self-thought full name even exists then we will return self dot full name otherwise we'll just return email I could say the same thing for the short name but I'll just leave and the full name so back in our admin the personal info stuff doesn't have to change the list search fields we can add full name in here now too if we wanted that and their real so let's try it again I actually don't think I have to run any migrations with the exception that I may have added blankies I'm not sure if I added those in this so let's let's go ahead and run the migrations and no I didn't have to make any changes because although I said it was required as far as Django is concerned the database didn't care if it was required or not so now if we go back in here we've got our full names and I can add them as needed so that also means that in my admin these little fields set things right here this is related to how I want to display it I don't have to go about this method right I can display it however I like so in the case of personal info I could just say full name you know and that will change that section name maybe I do that maybe I don't or I don't even have it in there at all and I put it up here and I just comment this out completely and then I can change some stuff so this is kind of up to you on how you want to like play around with the Django admin itself that's a little bit out of the context of what we're doing here this was just a lot more about how we can add either a required name or excuse me a required field in this case full name or a non required field again in this case full name now one caveat I've already said it before but we would if we want to really extend this user model or add things to it we might as well create a separate model that just inherits from it much like our billing profile right so much like this we would do the same sort of idea so let's actually run through and make sure that our project is now set up so we can kind of wrap it up on this user model so I go to products I add it to cart looks like my carts looking good I'll go to checkout I have to add in a shipping address I'm making up a shipping address I hit submit my billing address now I'm going to use the same my shipping address I check out thank you for your order looks like everything worked as normal let's just go in and look we've got our orders here great and then we have our shopping carts great so again like cleaning out the database allows me to run with things like this the last thing that I need to do for this user related stuff is our registration page as well as our login page we need these things to be different so that's something we'll still do if you have any questions on this let us know otherwise let's keep going so is our final step for this we need to update our register and login views and this will be something you could use regardless of if you're following along in this project so we'll go into our view here and we already have pretty much everything that we need set up to work with of course the exception of how this username portion works so let's actually try it out with how it stands and I'll do hello at team CFE comm with that password I hit load in what do you know it actually loads in so this portion or the authentication portion doesn't change it's exactly the same as it was before it's just the field that's coming through has to be a little bit different so in our form for login let's scroll down to it this should actually just say email now or we can we can give it the label of email and what that will do is when we log out it now just says email but of course I want to actually use an email field so let's call it email field and now it's it shouldn't accept just something like that I hit login at least in html5 it's making sure hey you wanna you want to make sure that you're using an email and then if I use something like this and hit submit it's gonna give me that validation error which is something we expect to see or should expect to see the same is true for our register form but we actually have a user create form already it's this form right here now is there anything in here that is different than our actual register form well yes there is one major difference and that is how it's saved right so it automatically saves it I may or may not want to automatically save it now in the admin I might want to save it so let's go ahead and copy this form and bring it down here I'm going to change it to being the now register form everything else is the same I want to make sure my save data is here the biggest difference on this one is we'll say user dot active equals two false so this is where I want to say check or you know send a email or a confirmation email so to double-check to make sure that their confirmation has been done like they've actually confirmed their email exists and that's pretty much it that's the only thing I'm really gonna change I don't have to worry about these clean methods or anything like that so I'm gonna go ahead and completely get rid of that because this is now doing it for us we've already seen that like we saw it before and then we can set whether or not this user is active much better registration field and notice it says now full name email password and password confirmation or confirm password that part is kind of up to you it's a little arbitrary there so let's go back into our view for register and now we've got a few other things in here well since it's a model form model form I can ignore this and just say form dot save that's it so let's try it out I'll refresh in here and I'll just say Justin Mitchell and ABC one two three four five six or whatever at gmail.com and I'll type in a password will hit submit I actually not sure if anything happened because of how my view is set up so let's go into the admin and just take a look I'll log in as my admin user which was hello at Team C Fe and we'll go to users and our recent user was created but they're no longer active so let's go into login and see if I can get that user to login let's see what it was again up to six so let's log in as that user I try to login it's not giving me an error but I do try to log in but it's not allowing it so let's log out and we've got our abc123 we'll login or try to it's not allowing it to happen why is that well that has to do with a couple things one of them the active model itself and I believe you more specifically is this act is active property so those two things are false right now for this particular user so this user is not able to log in so that is pretty interesting so that means that the confirmation portion would now be required let's go back in and make that user active so active user will just select active hit save log out log in again grab that user user password and now what do you know that user can log in so just slight little things that are nice about these custom user models so actually before I go away I want to improve these views I want them to be solid views into where they are real working like class-based views instead of where they're at right now so that's something we'll still do so stay with us so changing these items into class-based views is well it's fairly straightforward what we're gonna do is we're gonna import from Django dot views generic import the creative view and the form view so the create view is going to be for our register view so I'll do class register view and it takes in that create view the form class itself is going to be the register form I will also leave the same template so we'll do template name equals to this same template here and this should actually handle my register view so I'll just give the success URL going to our login page and that's it so this user model part I don't need any more so I'll comment this out and form our register view I'm going to bring it into my URLs so I'm from accounts not view instead of register page it's register view and then we will find accounts register where you live there is and then as view save that let's go ahead and take a look at it now going to register same stuff right so we can try it out with just some random oops messing things up here go so some random email some random password like learn code it submits the passwords don't match of course they don't learn code learn code there we go and it registered that user and moved me over to being at login cool so that part worked and my form I'm gonna take off this active being false stuff for the future this might be something we end up doing is is like doing that like registration or more advanced user registration related stuff this is not related to the custom user model itself this is much more about just general user registration so back into our view this is now our new register view very simple easy to use now we're gonna do our login view and I'll just call this login view and it's going to take in the form view and again the form class is equal to the login form the success URL well we might not necessarily need to set the success URL here but I will and I'm just gonna put it here for now as that and we're going to update our form method let's let's go ahead and make sure our login form it is still being called email or username let's let's change it to being email because you know that's that's what it is it's not a user name anymore so back into our view now what we'll do is say define form valid take in self we meet this means the same thing as this right here so I'm going to do the same thing as that right there of course this is now email instead of username and then username is equal to email or this is the way to authenticate because that's email is our username field and basically from here this is saying that the user is ready we could just say that the form is invalid so we can return super of this form class of login view self and then form invalid and just run that method if the user is none so there's probably other things that I could do to make sure that this was a little bit better but I'm not gonna do that just yet and I also want to have these redirect next related stuff so I'm going to bring that inside of this form valid method because that's where we're going to end up using it and instead of request we have to also set the request to equal to self dot request okay so we'll copy this and we'll bring this into our URLs again login view as view here let's try this out and login oh we need to get a template name that's a little bit stake there let's try that back so say template name is equal to the same as the other templates counts login ok and we try the again refresh in here hello at team CFE password learn code oops that's right this takes in itself and form and down here form invalid of the form let's try that again and we're now logged in so of course this makes sure that we have to save everything ok so now that we've got this let's try it out on our cart because that was the other big part of this so I add a cart go to the cart checkout again login with our email and password I hit submit it brings me back to that cart so so it's definitely working as expected right it's working the same way this probably could be improved in general but we want to have our methods as class-based methods that's that's typically actually I'll leave this commented underneath here that's typically where we're gonna want to work towards in general and and hopefully you see that this is not a whole lot different than what we did with the exception of that register view that's that's just a lot a lot cleaner all right so we're almost done the thing that we're not quite finished with yet is that actual verifying their email since our model is now based off of that email we definitely want to make sure that it's verified so these things right here we will actually want to still do and we're gonna have to create ways on doing that right actually gonna have to go about doing it inside of our course here inside of the e-commerce course those things will be done in a separate section in of itself because they deserve their own section to really understand how all of that stuff's gonna work so definitely check out the course if you are interested in that of course it's join CFE comm slash courses slash ecommerce we will have it in as an add-on much like what we just did was the custom Django user model thanks so much for watching I hope that you've got something out of this if you enjoyed this and you see how you might implement it please let us know in what way do you plan on implementing your custom user model hey thanks so much for watching I build everything that I can to help you guys launch your projects faster better and more often so please consider subscribing join CFE comm /youtube is a quick way to our youtube channel 20 feet comm slash projects and just join CFE comm that is where all of our stuff is and I really hope that you join us there as well thanks again so much and I can't wait to see you in the next video [Music]
Info
Channel: CodingEntrepreneurs
Views: 129,790
Rating: 4.9271121 out of 5
Keywords: djangourlshortcfe2017, django 1.11 tutorial, django tutorial, install django on mac, install django with pip, install django with virtualenv, virtualenv, python django, Django Web Framework (Software), Mac OS (Operating System), Python (Software), web application development, learn django, installing django on mac, pip, python package, django, kickstarter funded, beginners tutorial, trydjango2017
Id: HshbjK1vDtY
Channel Id: undefined
Length: 64min 40sec (3880 seconds)
Published: Mon Oct 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.