Create user profile using django signals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] world so uh in today's video we are going to create user profile model okay so previous user model was only containing users details like username email ID first name last name and password okay which are basically used to you know create users account and also log in user in our website so in today's video we are going to see that how we can fetch users details uh like there social media handle IDs and their location their bio Etc okay so first I will just you know recap the things like which was our last video okay so if you come to my channel and see Django tutorials okay Django 4.0 tutorials then we our last video was deploying our website okay and if you have completed this uh till here then you can continue from this video which I am currently creating okay so now first I will go to the Chrome and I will open this website I will give you this link in uh description box okay I'll just copy all this code come to vs code back and I will just open this dot kit igno file I'll remove these both lines and I'll just paste that over here okay and now after doing this I'll just close this file now in authors app in views.py we will just clean this code a bit okay so I'll just remove this third line because we are not using these both packages okay so I'll just remove it and we are also not using this import Django and we are also not using this login underscore required okay so you can remove this after this in authors app we need to open this models.pi file now here we will create model named user profile okay so here let me clarify one thing that we are not going to you know modify Django's default user model okay we are just creating one different uh database or a table which will hold all the additional details of the user so in this tutorial we are also going to use signals okay so first let us understand the structure of our whole execution plan okay so I'll just open whiteboard now here uh let's say this Square is our default user model okay so I'll just write D and u okay then this circle is our signal which we will create shortly okay so here I will write s and this another box is our user profile model so I'll write U P okay so now what we are going to do is that whenever the user is creating his or her account then this signal will be triggered okay when this signal will trigger we will automatically create users profile right so in Django signals are basically used for these purposes only okay whenever user creates any particular account or their login then we need to perform one additional task which is which is not you know having any relation with users but it is having relation with our backend code okay that at that time we can do this okay so we don't want our user to create their profile manually okay so we are going to you know do this automatic task I hope you got the idea right so I'll just minimize this don't go on my drawing please don't laugh okay now here in models.py file I will just create class and here I will say user profile [Music] and I will say models dot model okay now here I will say user equals to models dot I'll just disable this Copilot models.121 field okay now this one to one field will take user and I will say on underscore delete you will have models dot Cascade okay so whenever our user is deleted the model should be deleted right this user profile should be deleted now here we have one New Field to understand which is one to one field now what is this how it works so let us jump to whiteboard again okay so what does this one to one field do like whenever you create a particular profile okay so let's say this is a profile one right p 1 okay and we have a user account okay which is U1 okay so whenever the profile we create the particular profile which will be allowed to access only one user like if we create this P1 for U1 then this profile can be accessed only for U1 and this will be mapped only for U1 we can't allocate this same profile to other n number of users okay not this is not working like foreign key so in foreign key we create only one field okay one field and that same field can be used with multiple user okay so that can be used for you know this user then this user this user this user Etc right so in one to one field this field will be only having this user no one else okay I hope you got difference about foreign key and one to one field right so here it is showing that user is not defined okay so let us see that also so first we will import users import user model actually so for importing user model we have two ways so first way is from django.com trip Dot auth dot models import user okay this is first and another is from django.com trip Dot auth we will import get underscore user model okay so this basically brings out current user model okay and this will bring out a particular user model okay so you know this is recommended way the second way I wish which I shown you this is recommended way so here what I am going to do is I'll say user equals to get underscore user model right so this will bring out the model now our second fill is going to be bio equals to models dot text field okay which will be blank equals to true so I am doing this blank equals to true because this all these fields are not required okay if user wants that he should give his bio and present in his account profile then only they can add otherwise they can't right so this is bio then location equals to models dot character field and Max underscore length will be let's say 50 characters then blank equals to true so I'll say blank equals to right then we have birth date so I'll say dob equals to models dot date field right and here I'll say null equals to true and here again I am going to say blank equals to true then we have user profile image so profile underscore image equals to models Dot image build and here I will say upload to [Music] um let's say profile slash and blank equals to group okay then we have website equals to models Dot text field not text field URL field okay URL field and here I am just copying this blank equals to true because we are going to write this thing in every field okay then we have GitHub equals to this models URL field okay I'll just copy and paste that then we have Twitter [Music] again it is going to be same then we have [Music] what's other thing which we can fetch from user I think this is it only okay so we are I will just create string representation function so I'll say def underscore underscore HDR underscore underscore self oops self and here it will return formatted string self Dot user oops is a DOT username [Music] okay and here I will say a post office and profile okay now this has been done so here we are now going to create signals.py file okay so in authors app I'll just right click and say signals.by now in signals we will first import some things like we are going to import post save so from Django dot DB dot models dot signals import post save okay then from Django Dot conrip dot auth import [Music] get the score user model then we will have from Django dot dispatch import receiver okay then we will import our profile model so from dot models import user profile okay after importing all this thing we will say at the rate receiver in this receiver will take post underscore save comma sender foreign okay so this standard is going to be user and I will just say user equals to get user model okay then I'll create function Def and this will say create underscore user underscore profile okay and inside this I will send first parameter which is sender comma instance comma creative comma quarks okay now here I will just write if created okay if our user account is created then fire this command which will be user profile dot create sorry dot objects dot create and here I'm going to say user equals to instance okay now I will just print all these signals send instance and create it okay in our command line over here right so I will say print our sender is going to be sender then our instance [Music] is going to be instance okay oops [Music] here I will insane strengths with capital i then our created is going to be Boolean value but still we will print it created okay okay so once this has been done so in authors dot py come to apps okay apps dot pi now here we are just configuring our app that whenever you know this app class has been called also called our signal function so here I am going to say Def ready okay just import authors [Music] authors dot signals okay now we are all set to create account so here first I will migrate the model so I'll say python manage dot Pi make migrations oops [Music] so we have one syntax error okay we don't have syntax error so here in models.py since we have used this image field okay we need to in install this below Library okay so I'll just install that okay now I will just say make migrations okay the command has been created now I'll say migrate so migrations has been done now in admin dot pi we will register this model so from dot models import user profile and here I will say admin Dot site dot register user profile right and I will say python manager.pi then server open this in browser [Music] okay now I will just log in as admin so I am just going to go in admin panel I'll say admin and admin now in all users I will just delete every user except this admin user okay once this is done just go to our site and I will log out okay so now whenever we create this user we need to see some print statements over here okay so I'll just write yes but till okay here my new user ID will be yours only name is going to be Django world 21 at the rate gmail.com password will be so my account has been created now we should see some signals you can see our sender is user model okay then our instance is Yash I mean my username okay and created is equals to True okay so let's go over here and login again I'll say admin admin now in user profile we should see this Yash profile right so in this you will only see this user field has been filled up and other fields are still empty okay so this Fields is going to be created by a user only okay not by back-end system backend system is only supposed to fill this field okay not any other things so I hope you got idea right I also hope that you got these signals okay so this is very important and we are also going to see these signals in depth as we proceed our project further so yeah I have created this another video after a two months okay so we will keep updating this project okay so yeah this is it for today's video guys so in next video we are going to see how a user can update his all detail okay in from front end and we are also going to change this profile interface so basically whole this thing is going to be changed type so I'll see you in next one thank you [Music]
Info
Channel: Django World
Views: 2,697
Rating: undefined out of 5
Keywords: django, django tutorials, what is signals in django, signals in django, django signals, how to create user profile in django, user profile, user profile django, user profile model in django
Id: 0oKEyG4yNFg
Channel Id: undefined
Length: 22min 1sec (1321 seconds)
Published: Fri Jul 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.