Switching Username to Email in Django Authentication | Django User Model Customization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you how to create a custom user model and the easiest way to authenticate not by the default username field but by email so without further Ado let's get started in order to follow along you need to have any Django project with default authentication via username for this particular tutorial I'm going to use the project that is a part of my udemy course the link is in the description if you are interested before I'm going to talk about the two options that we have let's create a separate users application where we are going to place the custom user class in the models py file so in order to do this let's move to the terminal quit the development server from running and let's run python manage py start app users now let's add the user's application to the installed apps list so we need to find our main project folder settings py file let's head over to the installed apps list and let's place in users let's save the settings py file and let's open up the user's application and access the models py file and over here we are going to create a custom user class which can inherit either from abstract user or abstract base user with abstract user we are extending the existing key user model we are customizing the existing user model while with abstract base user we are creating everything from scratch so this approach gives us more control on what we are doing on what we are creating however it requires much more work so in this video we are going to focus on the first option so what I'm going to do right now is to bring abstract user so we need to import it from Django country Aus models and from here we are going to import this abstract user as you can see we have abstract base user as well if you decide to go with abstract based user you can also use the permissions mixing but this is a topic on another video I'm going to bring in just the abstract user and right now the custom user that we are creating will inherit from this abstract user as the next step we need to redefine the email field so the abstract user comes already with such Fields as first name last name password is stuff is active is super user and so on however the email by default isn't unique so we need to redefine it over here to be unique so we can use it as the username field so let's write email is equal to and then models email field over here we are going to set the max length to 100 and very important we need to set it as unique true okay that's the next step let's set it as the username field so we don't want to use username anymore we want to use the email and also let's specify the required fields here we need to provide a list of fields that are required when creating a user using the create super user management comment okay so I'm going to place here just the username now we can go ahead and save the Moto's py file and we can jump into the settings py and maybe somewhere let's say over here we need to tell Django that we want to use the user model created by us so the auth user model is going to be the one that we have in the users application named custom user let's save the settings py file as well now I'm going to move to the Run reset script and in the create super user section I'm going to do some notifications so this script as a reminder resets everything it removes the database it removes all the migrations it creates new migrations and then generates new dummy data and here I'm just going to paste a new line of code where we are importing the custom user model and then we are creating a new custom user providing over here the username but also we need to now remember to provide the email address and then of course password and the rest doesn't change so I'm going to save this and then I'm going to jump back into the terminal and execute this script so let's run reset sh all right so let's run the server and let's try to log into the Django Administration we see that we don't have no longer username now we have email so I'm going to provide the email and the password okay we are in and we don't see users anymore in the Django Administration so let's register the user model in the admin from dot models we want to import user custom user to be more precise and then admin site register custom user let's save this and now we see the users over here let's open up the users let's access the one user that we have and as you can see in this view looks a little bit broken it doesn't look the way it did previously when we had the built-in user model so you can see for example that we have the Boolean Fields staff status and active completely separate from the super user status so we can customize this on our own or we can bring in user admin and register user admin as well and this is the easier method so I'm going to go with this one from Django con trip Alf admin we want to import the user admin and let's bring it next to the custom user let's save this and as you can see now it's looking the proper way let's log out and let's refresh the page as you can see we still have the username over here so in order to change this we need to go to our core forms py login form and I'm going to comment out the username and here I'm going to place an email and this is going to be forms in mail field and the label is going to be email with quotation marks of course and max length I'm going to set it to 100. okay let's save this and now we see email and this is because if we take a look at the templates login over here we have placed field label so whatever we place in the label that is in the form it's going to be displayed over here as the label Okay so if we go to forms and change this to email Z over here we will have email Z so now the final thing that we need to do is to perform some modifications in our views py so we need to change a little bit the login View and the OTP view so first of all I'm going to cut this put it over here and then I'm going to comment this out and I'm going to import get user model so from Django on trip on trip out that admin but alph we want to import get user model and we are going to set the user to get user model this will give us the active user model all right as the next step we need to change the username because we are not using the username anymore we are using emails so I'm going to place an email and then form clean data email and then we are going to authenticate not by the username but by email so here I'm going to set email as email okay and then in the request session username I'm going to keep the username but I'm going to place over here the email okay then we need to move to the OTP View and over here we just need to make one notification and since we are picking up the username from request session that we have set over here what we need to do is to change the username as email okay so this username is being picked up over here but it stores the actual email so we are setting the email as in fact the email and yeah let's try this out will this work well we will find out soon so I'm going to provide my fake email address and I'm going to provide the password all right we have the OTP verification so let's grab the code let's paste it submit and there we go everything is working so this is it for this tutorial I hope you guys enjoyed it and hope to see you in the next one
Info
Channel: Pyplane
Views: 4,503
Rating: undefined out of 5
Keywords: django custom user model, django custom user model email as username, learn django, django authentication, django authentication tutorial, django email, django email authentication, django custom user model authentication, django custom user model abstractuser, abstract user model django, abstract user django, abstractuser vs abstractbaseuser, learn django 2023, web development, learn python web development
Id: hN0xbn4sENw
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.