How to extend the User Model in Django with AbstractUser

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys this is Luke I'm recording myself for the very first time so I would like to say hello to all my subscribers thank you so much for your support I know I'm not a big programming channel but each life is a subscription each comment gives me a huge motivation to carry on with what I'm doing so one more time thank you so much for your support and the next thing is that with this video I'm starting a brand new series called beyond the basics of jungle so in this section you will find hopefully essential content for you to become a middle level developer instead of doing all these projects that you can find all around the internet like to do apps or blogs we will create smaller projects that not always will make sense but they will teach you some of the most powerful and coolest features of jam and we will begin in this with this video with the custom user model let's get started all right guys let's get started with this tutorial so I have a blank window of terminal open and the first thing that I'm going to do is to deactivate the Conda environment next I'll go to the desktop and over here let's create a virtual environment called beyond basics or maybe beyond env alright let's wait a few seconds now we can go ahead and access this directory so beyond and V and let's activate our virtual environment so source bin activate alright so now we can install Jango all right so now if we run the comment bit freeze we have a confirmation that Jango has been installed so we can set up a Django project so Django admin start project and let's name it movie prod alright so right now we have in this directory over here beyond and V elements bin include lip and our movie proach so if we go to the movie barrage right now we have also a movie prod over here so to avoid the confusion I'm going to rename this one to source so I'm going to go up a directory and rename movie prod to SRC and right now as you can see we have SRC so I can go to the source directory and open up code Visual Studio code editor over here we have our main project folder and the manage py file which will help us manage called administrative tasks we don't have the database yet we will create one soon but before we do that let's actually jump into the terminal and create our application so we need to run we need to actually use the manage py file now python manage py start app and let's call it users ok so now we have a users application folder let's go to models py file and here we are going to create our custom user class so we can type class custom user will be the name and this will inherit from the abstract user so we need to import this model from Django contrib off and then models import abstract user so right now we can copy it and paste it over here so what we are doing right now is inheriting from the abstract user we are getting all the fields of the standard user model and we can add additional ones so in our case we would like to cut is director and is pro producer both of those fields are going to be boolean field so let's define them I'm going to set a value of defaults to false for the director and for the producer I'll change it to true alright so later when we will cover login functionality we will allow only the producers to live login to our system to our site the directors on the other hand will have some kind of a notification that you need to be a producer in order to access the site alright so what we need to do next is to save this models py file and jump into the main project folder open up the settings py file and add this application this users application to the installed apps list all right and also let's go somewhere somewhere maybe over here and let's write out and then user model and here we need to indicate what user model we will be using so we are referring to the users application where we created our custom user class custom user okay so we have the users application and in the models py we have our custom user so right now if we save this we can jump back into the terminal and run Python manage py make migrations and Python manage py migrate and Python manage py run server so we haven't registered our user custom user model in the admin yet but we can jump into the admin to see if everything is ok so first of all let's open up a new window and go to the admin over here of course we won't access the admin because we haven't created a super user let's do this right now python manage py create super user let's provide a name and a password alright so now let's refresh and try to login of course we need to write down Python manage py run server ok and one more time let's login and here we are so we don't have any users over here we have just the groups in the authentication and the authorization section so in order to have the users well we need to jump into the many of the users application and we need to write down from dot models import user custom user actually and over here let's write admin site register custom user this won't work entirely because you will soon see what kind of issues we will be facing but let's do this right now like this and then we will return and add some code to this admin py file let's refresh and here are the users so let me try to add one and as you can see this isn't looking that good because we begin adding the user from the password and then the username is somewhere in the middle over here we have the last login then we have the super status check but the staff status check and the active check is like almost at the end we have also our two checks for the is director and its producers so those are the fields that we added but there is no information what is their purpose what they are referring to so this layout over here is extremely confusing and let's fix this let's jump back into the users application and let's create a forms py file where we will begin by doing some imports well first of all we need to import the form so from Django import import forms the next thing that we want to import is the user creation form and we can get it from Django contrib off and then forms user creation form and finally we need our model so from dot models import custom user so now we can define a class let's call it cost user creation form and this will inherit from our user creation form over here with the class meta we will indicate the model so we are referring to the custom user and we want to set the fields to be equal oh alright and so now we can jump back into the admin let's bring this form to our admin so from dot forms import custom user creation form and we will also need user admin so from Django contrib again come trip again out but this time admin import and we want to import the user admin so again we need to define a class let's call it custom user admin and let's pass in this user admin this class is inheriting from the user admin and the model will be the custom user and we will also want to specify the form so we need to put in add form and set it to custom custom creation custom user creation form okay and now we can register this custom user admin next to the custom user so let's save it let's refresh and as you can see we have a pretty good-looking add user template it's looking actually as the standard template so let's see if this is working as we wish you to put let's press save and everything seems to be normal except that we don't see our custom fields maybe for a second you thought that we finally fix this problem actually there is still one more step that we need to take in order to have this working properly so over here we are going to define the field sets and we need to open the top oh right down user admin and field sets and after the comma let's open up another couple here we will set the header so basically we are setting this right now and I'm going to put in user role and this time we need to open up a dictionary write down fields and what fields do we want to have in this section well we want to have is director and is producer all right so now we can go ahead and save this hit the refresh button and as you can see everything is the way we want it to be so we have the user role over here again we we set the name over here so if I would put in Rolex after refresh we have user Rolex okay yeah we can now go ahead and test this out so I'm going to refresh so we have the user role not user Rolex and I'm going to set yeah we can leave as this producer so I'm going to simply save this and there'll is everything is working as we want it to all right guys so this is it for this video I hope you enjoyed it and if you did please subscribe to my channel in the next part I hope it will get even more exciting so thank you once again and see you soon take care and bye bye
Info
Channel: Pyplane
Views: 16,156
Rating: undefined out of 5
Keywords: django, python3, abstractuser, extending user model, how to, django tutorial, django intermediate, beyond the basics of django, django programming, django advanced, django made easy, learn django, python django, django project, custom user django, extend user model django, python django tutorial, django project tutorial, django projects examples, django project ideas, python django projects, python django website, web development projects, User model
Id: 1BeZxMbSZNI
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Wed Jul 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.