Build And Deploy a REST API with Django REST Framework #3 Creating a Custom User Auth Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to this video in this video i'm going to go ahead and set up our database model for the previous video we created our different applications which are the authentication as well as the order or the order application so i'm going to go ahead and set up our database model so right here i have a simple model of how our database is going to be we're going to have two tables we're going to have a user table which is going to be the table containing of all the information that's going to be specific to the user accounts as well as the order table which is going to have that different information for every order so right now we see that our user table is going to be a custom user model containing a username and email a phone number and a password it's also going to contain other extra fields as jungle provides now the general default authentication user model is kind of sufficient for most use cases but then in case you want to customize it we can build on top of it so that you create our own custom user model as you're going to see we also have our order table which has the different fields such as the size the order setters the flavor of the pizza the quantity and created add updated that which are dead time objects we also have the user id which is just a foreign key to the user table now for that order status we have uh these different status for example when we make an order it is pending it hasn't been attended through we can also have that order in transit we can have the order delivered i also have the pizza sizes which are small medium large and extra large so we're going to look into how to implement these tables in this video so let's go ahead and do that so i'll head over to our visual studio code and right here i'm going to go to our authentication so within our authentication usually i'm going to trade a different project for our user creation so i'm going to head over to modules.pi within our authentication and then we're going to go ahead and create a custom jumbo user model so to do that what i'll do is to import the abstract user class from jungle. from the jungle authentication models while this is from django we are going to go to a contrib and dot off then dot models we are going to import our abstract user so this is going to be the model from which we are going to inherit and then we go ahead and build our custom user model so we're also going to create a manager that's going to dictate how we're going to create our super users as well as the normal users of our application so i'm also going to import the base user manager class from jungle that's going to help us to do this so i'll say from jungle dot contrib so in this case we're going to say it's also going to be from oauth so then i also said the best user we are going to go ahead and import our base so it's going to be base user manager and after importing this we're going to go ahead and make sure that you create our custom user model so the first thing is to create a manager and the manager is going to basically dictate how our user objects are going to be created so the first thing i'll do is create our base user class so i'll create our custom user manager so i'm going to call it custom user manager so this customizer manager is going to inherit from this user manager and after viewing this from each override two methods so the methods are going to override our create user method as well as the create super user method so the first thing i'm going to do is to create a create user method which will take itself which is the object that shall be creating and then it's been taking the different fields the different required fields so in this case we're going to have the email as well as the password then you shall also supply extra fields so there are going to be those extra fields uh associated with that user model so you're going to also provide them so it shall provide extra fields so i'm going to pass this for now and you're also going to create our so let me invite this so that you guys can see so you need to also create our super user so our create super user i'm just going to take in the same fields so i'm also going to pass this for now now the first thing we're going to do is to go ahead and create our create user field so the correct user field the create user function or method in this case is one that's going to allow us to create a user object so it means taking the email the password and other extra fields as you're going to see so let me first close this okay now after closing those now we have our create user field so within our user field we are going to check if the email is now and then raise the value error in case we have not provided an email so i'm going to import uh you get lazy text you get text lazy from from jungle translation so that it tells us to throw up this value error so i'll come at the top of our code and while say is from jumbo dot utils so i'm going to go to translations so i'm going to import you get text lazy as underscore so what the first thing we're going to be able to check is if we have not provided an email so i'll say if not email what i'll do is to come and basically raise a value error so i'll say is raise a value error and then what i'll do is to basically give uh an inappropriate so just like you see uh an inappropriate adjuvant value of correct type so in this case we don't have an email so i'm going to raise an error in case they haven't provided an email so i mean it said that email should be provided and right after doing this then the next thing we're going to also do is to normalize our email so normalizing the email takes in the email that you provided and then turns the domain part of the email into lowercase so i'll do is to come and say that our email is going to be go to self dot normalize email then we're going to pass in the email that we provide now after doing that we're going to create our user based on our email and our extra fields so what we're going to do is to say that our new user is going to be equal to self dot model so we're going to take in the model that this manager is going to work on which is going to be our custom user model and then what you're going to pass in is that email so we have an email right now and then the extra fields so now after passing the email the next thing we're going to do is to set a password for this currently created user so we're going to say new user dot set password and then we're going to say that we're going to set the password so the set password function basically gets the new user and then hashes the password for them and then stores that password in the database so we're going to create all that new user dot save and finally save this user to our database now this fashion is just simply going to return the newly created user and after doing that then let's go ahead and implement our create super user function so the createsuperuser function is going to go ahead and create a superuser for us and this is only called in case we use the create user command with python money dot py so i'm going to go ahead and do this so the first thing we need to do is to set our extra fields so here our create user function basically takes care of our required fields but the credit super user takes care of both that create that required field as well as those fields that are extra filled so in this case we're going to set our extra field so the first thing we're going to do is to set so i'll say extra fields dot set default and then we're going to pass in the key so in this case the first extra field they're going to set is is staff and let me set this to to true and what this is doing is to set that extra field is staffed true which means that our super user is a staff user so also we need to set another extra field so i'll say hr field dot set default and then in this case we're going to set the superuser and we shall also set it to true so i'm also going to do the same thing for uh is active so i'll say set default sorry for that so this is going to be is active and then i'll go ahead and set this to true now after doing this then what i'm going to do is to check if any of this is left as uh false then raise a value error so in this case i'll first check so i'll say extra fields dot get is tough if it's not equal to to true so if i i can say is not true this is basically checking if this boolean is not true then what we shall do is trace our value error so minute raise our value error and then this value error is going to take in so i'm going to use view get text lazy and then i'll basically say uh you message actually what i'm going to do is to say that supply user should should have this stuff as true so i'll do the same thing for the other extra field so i will say if extra fields that get is surprising is not true i'll read the same value error so i'll come and say raise and in this case we're going to raise our value error which is that and also going to do the same thing for his active so i'll say if extra fails for gate uh in this case what you're doing going to get is uh is active so what i'll do is i'm and say it's active then it's not true so what we shall do is to raise our value error and then this is going to be uh surprise i should have is active set to true now after checking for all these and setting all these extra fields the next thing we're going to do is basically make use of our create user method to create a super user with the extra fields that we have set uh being equal to true so we shall simply come and return self and we shall call our create user method so we're going to call create it's going to take in the email and the password has given as parameters right here then plus the extra fields that we have set right here now after doing this then what you're going to do is to basically implement our custom user model so i'm each found just below our manager and the first thing i'm going to do is to create a class so this class is going to be our user class and it's going to inherit from abstract users when we say this is going to be from abstract user so after inheriting from abstract user then you're going to go ahead and implement the various fields here so the first thing we're going to do is to implement our username field so our username is going to be models dot filled and then right in here we're going to set at a max length so i mean say that the max name is going to be equal to 25 and then you should also have is being unique social set that unique is going to be true and i'm also going to provide that email so i'm going to come and say our email is going to be what those dots so in this case we're going to call it an email field so in this case we're going to have a email field and then we shall set the max length of this so i mean say max length is going to be equal to so let's say the longest inverse should be 80 characters long and this is also going to be unique so against it is true true and right after doing this then we're going to go ahead and basically also set our phone number so to use for numbers we're going to make use of a special package that's called jumbo phone number field so let's go and look at it so we need type jumbo phone phone number field so we're going to head over to that documentation so the jungle phone number field allows us to implement a phone number field that gives us the ability to to basically check if our phone number is valid or not so this field is specific to only saving for number fields so let's go ahead and look into how to install this so i'm going to come right here and the installation is as simple as copying this i need to copy this command and i pull up my time you know and then within our time you know within our virtual environment we need to paste this command this is going to go ahead and install our jungle phone number field so after installing our jungle phone number field we're going to go ahead and basically implement our phone number so our better field for number and then this is going to be equal to so i'm going to input the phone number field from jungle phone number field so i'll come here and say from jungle actually this is going to be from font number field then dot model fields so it's going to actually be from mod of let me check here so this is going to be from phone number field.model fields then you're going to go ahead and this is going to be model fields we are going to import that phone number field so this is going to be our model field for phone numbers so it's going to be um for number field so i'll come and add that field here so this is going to be our font sorry for this one number field so i'll come and basically add this so here we're going to specify the following so i'm going to say that we're going to have now as false meaning that we shouldn't have this as now then we're also going to have this as unique so we're going to set it as unique so after setting this as unique integrate also basically since you are building on top of our user model then you do not require to have an extra password field as it's going to be part of our user model so the next thing i'm going to do is to go ahead and basically go and create a field that actually a method is going to return string representations of our users so what i'm going to do here is to come you actually see it's going to be phone number field if i check here i have our phone number filled yeah so after doing that then i'll just simply create our str some integrator method it's going to be our data str method and this standard str method is going to take itself so it's going to simply return a string representation of every user object that you're going to create so in this case we're going to just simply return ring tray time f3 so this is going to be a user object and then we're going to simply return self dodge email so after creating our custom user field we may also need to create uh the required fields as well as to set up our email field now in this case we're going to use our email field as our our username field as our email so that you may use uh using our email to login into our system so i'm going to do is to set a username field so this is going to be our username field and this is going to be our email then i'm also going to set this screen to be email then i'm also going to go ahead and set our required fields now these required fields are the ones that you shall require i use that to provide in case we are sending up a user so our comment say required field and this is going to be a list of required fields now in this case we're going to provide the username as well as we're going to also provide our phone number so our comments say font number now after doing this the next thing we're going to do is to basically go ahead and migrate this into our database so every time we create a model with jumbo we go ahead and migrate this into our database so that we we might we create a migration for it and then we migrate that migration into our database so let's go ahead and do that so to do that i'm going to pull out many time you know and the first thing i'm going to do is to run the command python3 my new py and then make my version it's going to be make migrations and then i point to the specific app that contains the migration that you want to create so in this case it's going to be authentication so after doing this i'm going to press enter so we have modulus not defined actually what we did is uh this is supposed to import modulus as a from jungle dot db we are going to import our models so you need to come and run this so when you run authentication so right here we have uh we have a specific error let's say the vast access of authentication industries are the groups clashes with reverse accessor of reservoir groups so to fix this error what you're going to do is to go ahead and set up our oauth user model within our settings so the earth user model setting basically specifies which kind of user model that you're going to use which kind of customizer mode you're going to use so i'm going to head over to our settings for the p right that's within our pizza project so within our settings i'm going to go and add one important setting so that important setting is going to be our oauth user model and then we set that up is to rate that app in which you have our default user model so that is our authentication app and then you go ahead and specify which model is going to act as our custom user model sources dot user so when i save this i'm going to run our migrations again so anyone i'll make my aggressions so in this case it's going to go ahead and create our user model migration so our migration is located within our authentication and then within our migrations so if you go and look into our migrations file right here we have the different specified so right here as we can observe we have the different fields that you specified as well as other extra fields that you specified in our create user function so when i close this we're going to go ahead and write this one to our database so the first thing i'm going to do is to run python3 i need the poi and then migrate so this is going to go ahead and basically migrate our database right here we have uh migration1 initial which basically which is inconsistent so it's basically clashing with our initial migrations so to get rid of that the beauty with that jungle is we can use an escalate database for for our development so what i can do is to actually go ahead and remove our database i'm going to delete our database and we create it again so to migrate our database i'll just run python 3 monitor pwi and then migrate so this is going to go ahead and create our database tables once again so right now we've been able to create a custom user model so the first thing i'm going to do is to create our admin so i'll go ahead and say python 3 monitor py and then i'll go ahead and run this with create super user so this is going to prompt us so we have our email summary provider email so we're going to provide the email as admin uh app dot com and universe we're going to provide a username so there is going to be admin so i'll provide the phone number so let's say two five six seven zero nine nine eight four actually it's going to be seven zero nine nine eight four five six seven i also provide the password and then go ahead as we'll provide another password so right now we've provided a password that is too common that i'll say yes so this is going to go ahead and create a super user so to create to access our admin dashboard you're going to actually djangoconsider an admin dashboard that you shall use to create uh green update and delete our model so i'm going to access that with running our server so i'll run python 3 money to pay rent server and then right here this is going to go ahead and run our server so i go to localhost 8000 so when i go back to our browser which is chrome so in this case i'm going to go to localhost 8000 slash admin and then this will give us access to our administrative login with the credentials admin at app.com and our password which is password one two three so when i log in this is going to give us access to our admin dashboard so after doing this we are going to go ahead and basically register our user model so that we can be able to control our user accounts on our admin dashboard so to do that i'm going to go to our visual studio code and then within our visual studio code i'm going to go to our admin module within our authentication app so within admin the first thing i'm going to do is to basically add to register our model so i import our model which is our custom user module so i'll say from uh dot modules meaning from the models module within our current folder i'm going to go ahead and import the user model so right after importing the user model i'm going to go ahead and register it with admin dot site dot register and then i'll go ahead and register the user model so when i save this our development server is running and head over to our to our jungle admin so in a refresh we now have access to our custom user model so if i check for the current users we only have the admin user created so we can see the various information that the user has so in this video we've been able to set up our custom user and set up our database as well in the next video i'm going to go ahead and create a quota model so thank you for watching and if you've enjoyed this video all you can do is to leave a like please comment on what you feel and thank you for watching see you in the next video bye
Info
Channel: Ssali Jonathan
Views: 30
Rating: 5 out of 5
Keywords: django custom user model, custom user model django, django custom user, django, django custom user model signup, django custom user model tutorial, django user model, django custom user model stackoverflow, django custom user model authentication, django custom user model email as username, django tutorial, custom user django, custom user model in django, django custom user model admin, django custom user model example, abstract user model django, extend user model django
Id: 7fcbCdntllk
Channel Id: undefined
Length: 24min 46sec (1486 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.