Customize User Profile Model - Django Blog #27

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody me calm and in this video we're gonna start to extend out our user profile for a blog with Django and Python I guess like I said in this video we're gonna start to extend out our user profile add more fields and things to it but before we get started if you like this video want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out coding me comrade dozens of courses with hundreds of videos that teach you to code use coupon code youtube wanted to get $30 off membership that's all my courses video some books for one time VHS $49 which is insanely cheap ok so in the last few videos we've been kind of messing around with our profile or user profile and we have some things here we've got you username first name last name email the password and some other things that are like administrative type things and the date joined right so these are all the things that come with the Django authentication system the user model that gets set up automatically with the Django authentication system and this is great this is a great starting point but in the real world you probably want to add more things to this you know you might want to have an author bio you might want to have a profile picture there's all kinds of stuff you might want to add to this and it's kind of actually a little hard if not impossible to add things to the user profile instead what we want to do is create a whole other model and then just associate it with our user model with a one to one Association right so if you're familiar with database associations this is gonna be super simple if you're not familiar with it it's also gonna be super simple it's pretty a pretty easy concept so that's what we're gonna start to do in this video now there's a lot of moving parts to this in order to get this all up and running in this video we're just gonna sort of set it up and discuss it a little bit and then move on from there so we've got our edit profile let's head back over to our code and it's clean some of this stuff and what we want to do is head over to our blog app here and inside of here see we want our models PI file now we've got several models going on here we've got our category we've got our blog posts model and stuff like that now the user model is not here the user model is in our members directory under all of our Django authentication stuff right so we probably want to create our model here I guess it probably doesn't really matter but let's go ahead and just add it here so I'm just to create this and now in the past we've already imported our user model because we used it other places inside of here down here when we set up our likes system when you like a post right so make sure that this is is set up so from Django can't rib auth models import user if you didn't create the like system and you don't have this line here add it right now because we definitely need this so all right let's create a new class and let's just call this profile so our user profile or you could call it user profile or whatever you want to call it I'm just gonna call it profile and this inherits models dot model as as usual here you can see all of these sort of do that right just normal now inside here we need to associate this model with our user model and we we do that with a one to one field that one to want an association so basically what that says is hey this goes with the user model right sort of so we can call the same thing we want let's call this user and this is gonna be in models dot one-to-one field and the one there the O and one the t and to the other oh and one and the F and feel are all capitalized so what do we want to associate this we want to associate this with our user model what one-to-one Association do we want to create between this and our user model and remember this user model is in our Django authentication system that's in our or let's see members directory right here all right all of this stuff so we'll look at this in a bit so okay we also need to say hey you know we've already got some users set up so they're not associated just yet so let's put null equals true and then we also need to do on underscore delete equals models dot cascade so basically if we delete our user all of the users profile stuff will also get deleted automatically and we did the same thing down here when we did this guy and and so that's cool so okay so now this is real all we need to do to associate these two models so now what else do we want to add now we're gonna add a bunch of stuff probably a profile picture and all kinds of other stuff but for now let's just do a bio right and I can just come down here and grab for instance like our old body we use a models text field that's sort of like a box you know so we can copy this and I'm just gonna paste this in oops one too many equal to size okay so for now we're just gonna leave it like this we'll just have a user bio right simple and like I said we'll add on to this in the future but for now that's fine so also we want to be able to see this in our min area and make it sort of more user-friendly so like down here when we did this little guy to return the title on the admin page itself so it doesn't just say object on the screen or something like that let's just go ahead and copy this and add this in here be sure tab over correctly and instead of returning self title we want to return self dot user and this one's kind of a weird one let's go ahead and wrap this in a string function otherwise we might get an error because this is more like an object string so we want to make sure this is a string so all right let's go ahead and save this now while we're at it while we're talking about the admin area let's just go ahead and add this profile model to our admin area so let's head over to our admin dot PI section and up here we want to also import profile and then we need to register it so we just copy one of these paste it in and change this to profile ok so now this will show up on our admin page our men's section right so ok we've created a pretty major change to our model that means usually that we need to make a migration and push the migration so let's do that real quick so head back over to our terminal ctrl C to break out let me clear the screen here and the command is always as Python managed PI make migrations we've done this a bunch of time so we've got our migration and then finally Python managed PI migrate and that pushes the migration so ok now let's go python managed a PI run sir to run our server and let's head back over to the website and let's log in as admin and now let's head over to our admin section remember this guy and now we see profiles now if we click on users and for instance and men there's nothing in here about a bio just yet we could add it later but if we come down here to profiles we see there are no profiles yet so let's add one and which user do we want to add it for we'll sleep use a let's added to user admin so our logged in user now this is my admin user bio ok so we go ahead and save this and here we now we see in men and we can click on it and there it is so if we want to come back here and add another one we can pick one for Bob this is Bob's bio and now of course in the future we'll create this to where this can all be done on the website itself but for now let's just knock these out real quick just so there's something there and this is Steve's bio save this so ok that works so how do we now access this stuff well let's head back to the website real quick and let's hit our edit profile page and let's pull up our code and look at our edit profile page and that is gonna be in our members directory I believe in our templates registration edit profile and if we just want to look at this to see that it's in there and then it's working which is all we really want to do right now let's create a couple more line breaks here and let's just put this up on the screen so we can call user dot profile and then dot bio now it's user first because this is our user model and we're getting all this stuff on this page from the user model right and then dot profile because that's what we called our model spy class profile I know it's capital here but we reference it as lowercase and then inside of here we can reference anything that's in that model by slapping another period just calling it in our case we only have one thing there so bio is really the only thing we can't access so let's go ahead and save this head back over here and hit reload and this is my n men user bio and there we go so we come back here and click on profiles again and click on it man this is my administered by oh I just updated it alright save it head back over here hit reload I just updated it so there's our bio now obviously this is probably not where we want to put the bio and you know we need to set up something in here so that we can edit it but in this video I just wanted to talk about the user Association associating another model with our user model and then sort of getting it up and running and that's how you do it and you know in the last video we looked at uploading images it would be really easy now to create a little profile picture we'll probably do that next and we're coming right along so that's all for this video if you liked it be sure to smash the like button below subscribe to that channel give me a thumbs up for the YouTube algorithm and check out economy comm or you can use coupon code youtube one to get $30 off membership pay just $49 taxes all my courses hundreds of videos and the PDFs of all my best-selling coding books joint over a hundred thousand students learning to code just like you my name is John elder from Kota b.com and we'll see in the next video
Info
Channel: Codemy.com
Views: 16,436
Rating: undefined out of 5
Keywords: django extend, django extend user model, django extend user, django extend user profile, django extending user model using a one-to-one link, django extend user object, django extend user creation form, python django user profile, python django user management, python django user model, build a user authentication web app with python and django, python django user authentication, python django user registration, django extending user, django extending user model
Id: Alua227cOmY
Channel Id: undefined
Length: 10min 31sec (631 seconds)
Published: Thu Jun 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.