Django Tutorial - Creating Custom User model in Django website #4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to part 4 of web development with python and django tutorials here in this tutorial we'll check out the admin functionality of django and cover its benefits before continue please like and subscribe my channel this will really help me to continue creating these tutorials thank you for doing this okay in the previous tutorial we saw that we could create records in database by using the jungle show but this is not the fastest and most convenient way that we would like to use all the time you usually want to get full access to manage the things through the user interface here comes the django admin panel we only need to take all the benefits it gathers uh here is an example every jungle project should use a custom user mode the official documentation of django states that it's highly recommended where it is yeah if you are starting a new project it's highly recommended to set up custom user model even if the even if its default user model is sufficient for you this model behaves identically to the default user mode but you'll be able to customize it it in the future if the needs arises that's what this tutorial is about and the all right i learned this the hard way i created this website from scratch i'm talking about my pi lessons.com website uh if if you haven't seen this this one where i have all the functionality for sf blogger and this is cool so i created this website from scratch by using the default user motor there were many relationships with a profile and another table where i stored another user sensitive information my code complexity grew because of the additional user relationships added by time and then i decided to create a custom user while doing this in a project middle stage i faced many issues finally i deleted all the databases and wrote code to correctly move data from the old database to the new one and i encountered more problems than you can even expect so i hardly recommend starting with the custom user from the beginning this is all also mentioned in jungle documentation in this page due to limitations of django dynamic the dependency feature of swappable models the modal relevance references by off user model must be created in the first migration of its application it's usually called the initial otherwise you'll have dependency issues this sounds good yeah that's exactly what i faced and i faced many problems this is the reason why i'm creating this tour and why i'm talking about this so i could help you to avoid this issue it can be priced in terms of time if you move on with default user model but i'll go a step further and say without hesitation you are straight up crazy not to use a custom user model upfront remember my words you'll regret that if you are watching this tutorial you might ask why do you need a custom user model in django so uh when starting approaching with a custom user mode stop considering whether it is the right choice for you for your project or not keeping all user related information in one model eliminates the need for additional or more complex database queries to obtain related model data in the other hand storing application specific user information in a model associated with a custom user model might be more appropriate this allows each application to specify its users data recommends without conflicting with or relating the assumption of other applications it also means that your user model should be as simple as possible focused on authentication and meet the minimum requirements that django expects customized user model to meet so i'll show you an example with a standard user model we have only specific fields here and other we would like to have user profile description for example image and etc while using the standard user model we must create another profile user model where we will store this information but while using the custom user model we can hold all this information in a single table in the single row so i think it's clear why we need a custom user model in django this tool will teach us how to create a custom user model and it's api in general so let's go ahead to the coding stuff as all of you wait same as we did in our second tutorial where i created our first application we need to do the same stuff now we create a users application so uh i i write up python manage dot but manage dot pi and start app and i'll call it users yeah as you can see it appeared here that's easy so so now open the project settings file and find the definition for the installed apps it should be here and then add a new line at the end of list as i'll i'm showing you right now users and this way we'll register a user's application but now it's different compared to our main application we need to create a model for our users so straightforward let's go to our user's new application to our models and let's do something here so at first it's required to have some kind of authentication so i i need to ins import from django dot contrib of models i need to import abstract user you can see there's two of these abstract and base user i decided to use abstract user but it's also possible to use abstract base user by default abstract user doesn't have email status and description fields they will be customized for my purposes later there won't be any issues with adding more of them for us so the default jungle user model users are using it to identify the user uh during authentication uniquely to use an email address instead of username you must create a custom user model by splitting it into abstract user or abstract based user so you you need to test this by yourself to understand it better way but in simple terms there is two options there is abstract user we use this option if we are satisfied with the existing fields in the user model and want to remove the username field an abstract based user we use this option to start from scratch when we are creating your brand new user model with all new fields here i need to create a class that is called custom user and here is my abstract user first i will i'm creating a custom field that i'm calling uh status and here i'll create a three state sets and this will be regular that is equal to regular and there will be two more let's create them sorry like that and and i'm creating subscriber subscriber subscriber and additional one i'll create a i will call it it's it will be an admin sm moderator so moderator let's go now i need to create another field that is an email field and models dot email field of course jungle has it and we want it to be unique as true next i'm creating a status and oh sure what i pressed accidentally doesn't matter for now let's exit this status will be models dot char field and here will be max length of i don't know 100 here we choose our status to be uh oh yeah we have to see the choices choice choices is equal to status this means that we'll have a drop down list and our default will be regular [Music] like that now we need another field that we'll call uh this script description description and this is a simply model's text field text field with the name of description and max maximum length will be not not too long single hundreds that's our profile custom description about us default will be blank and black blank equals to true that this means that it can be empty and as i said every modals it's recommended to have some kind of string field as a return so return self dot username sometimes you want to write an email sometimes username it's it depends on what we want so i i choose to use this one so that's cool uh i finished this one and now we need to move to the admin page here we need to register our new custom user so i add a space here so from models import customizer and we need to register it so we do this by admin site register and we call it custom user that's like that now we'll see our registered users in the admin panel when we log in before making the migrations we need to register our users model in settings we are adding one line additional line in our settings it's a must and you can't skip this without this it will it won't work and it's called off user model and it's equal to users dot custom visa you can't make any spelling mistakes or whatever so we are real close to the end because we already had a main application for which we already made a migration in the previous tutorial we need to remove all files related to this migration this is the consequence without even doing anything with our authentication and users when janko does it its initial migration it records that we use the default method instead of custom one so now you might understand why it's a bad idea to move out to a custom user in the middle of the project so we remove all the migrations in the main file which we have here i have only one we'll move to trash and we remove our database so we'll create a new one so as you remember i have only one record creator here that's why i haven't i didn't waste my time creating more so let's go here database sqlite let's move to trash now we can make our migrations python managed by my make migrations this error sounds reasonable sometimes it you can't understand this but right now i know what is the problem this is because i haven't removed the pie cash and yeah i need to do that right now as you can see don't worry they'll be created again when we run our stuff move to trash okay now it should work as you can see it made the migrations and without any errors so that's cool and this means that we can move another line with migrate migrate and these migrations were applied also so now we need to create our custom user so we again call our manage.pi and there is a command create super user and now i'll create simply a test user where i'll use test as test.test i use this to to simply to pass the email test and password i also use test as test and yeah it's really simply i just not to forget this stuff i create them really simply this cpu user is like other users and django user objects come with email fields django has built-in reporting features that will email your super users when exceptions are hit which can be really helpful so more information you can of course find here on this uh email reports uh page of course all that add this link to the repository of my textbox tutorial you can visit it later so now we can try to log into our website let's open it and i have here and we navigate to the admin stuff it doesn't exist right that you're right we move to the ah i need to run the server run server that's right now let's do this again i don't i will forget so right now we have our login page easily isn't uh but i press test and pres that my password is also a test right now so we can try to log in and cool it works right now we see that users and if i would click on it what i will see i will see my user test that's cool isn't it and that's really really simply and in the future tutorials i'll show you how we build on this that will be also quite interesting and easy we'll create articles and sears in our next tutorial through our admin panel of course it will be also really interesting because as you can see there is no possibilities to do that with our functionality even if i go to home there is nothing else i can do okay well let's see if i go to groups also nothing new but yeah nothing nothing we need to add this functionality but for example if we would like to add new user that's uh easy we type details we need here and it will create it manually but we'll never do this so in this tutorial we looked at how to create a custom user model so that we could add additional fields to our primary user identifier in the future registration tutorial you'll see that we can benefit from this model while allowing users to log in using username or email so this is sometimes really useful and a lot of people don't know how to manage this and you'll see that it's pretty easy when we use our custom user that's it for this tutorial please like it subscribe share and this of course should really help me to reach more viewers subscribers and so on i would really like to help you to grow with my own tutorials not only django but i plan to release a lot of deep learning machine learning on computer vision and etcetera guitar final code for this tutorial and examples including templates views urls and all the required files are on my github page you simply go there if you if something doesn't work for you you clone it and download and you should get the working vision um i'm splitting my files to each tutorial so this will really helpful so that's it for this part and see you in the next tutorial see you later my friends bye
Info
Channel: Python Lessons
Views: 14,387
Rating: undefined out of 5
Keywords: django custom user model, django tutorial, python django, Python tutorial, learning, Python basics, Python introduction, Django, Flask, HTML, CSS, Website, Django first app, Django website, First app, Django models, Django views, python django tutorial, django models, django tutorial for beginners, web development, django course, django project, learn django, custom user model django, django email login, django user model, abstract user model django, django custom user
Id: _tHabkMKh98
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Mon May 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.