Extend Your Django User Framework With a One To One Field

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone insanity from pretty printed here in today's video I'll be talking about how to extend a user model in django by using a one-to-one field so if you just want to add some basic fields so whatever represents a user in Django then you can by using the technique that I show you in this video this video is from a course called understanding Django that I have if you want to check out that course you can go to the link in the description below and you can view it and see if you want to join so I hope you enjoyed this video and thanks for watching ok so we're able to bring in the email and the first and last name fields but what if we want to create some other fields that are related to a user in some way well we can do that as well in Django and the way we're going to do it is by creating a new model that has a relationship to the user model now there are several ways that you can do this overall process but the once when a relationship is pretty easy to understand so that's the one that I'm going to cover so the first thing that we want to do is we want to create a new model and this model is going to be a model that holds the additional information for a user so let's call it something like user profile so I'll create a new class here called user profile and it's going to inherit from models stop model ok so it's just a regular model so the first thing I want to do is I'll use something called a once in one field so I didn't cover this earlier because it's not very common but it's very useful in this case and that basically means that it's almost like an extension of the original table so one user will have one profile so it's not like a once a mini where one user can have mini profiles it's just one user to one profile so that relationship Django can handle directly for us using the one-to-one field so to capital o is a capital T in a capital F the ninety's pass in the model that I'm interested in using so from Django contribs I'm going to import user and then I'll pass it in here because I'm in a sense extending the user model except I'm not modifying it directly so now I can add in some fields that I'm interested in working with for this particular case I'll add location and age just to keep it simple but of course this can be any profile information you want in your app so a char field for location I'll say the max length is going to be 30 and then an H and this will just be an integer field and then now define the stair function so source term method self and we'll say return self dot user username so whoever the user is it's just going to return their username here we turn something a little more specific but you know I'll just keep it simple so now that I have this I want to add this to my admin so admin dot sight dot register and then I'll import that model that I just created so use your profile see user profile just like that and now if I go to my admin dashboard so let's see should have an on delete for the once one field so we'll put on delete oops if I can spell delete so on delete equals models dot cascade and basically what this does is whenever you delete the user it automatically deletes the user profile for you so that's how the relationship is maintained because you wouldn't want to delete a user then you have this profile for this user that no longer no longer exists so that's why we have the on the lead here so I think I have everything now and if I go to the admin dashboard and login I see user profiles here so it tells me no such table example user profile so we know what that means that means I need to make migrations and then migrate so make migrations and then migrate and then I can start the server again and refresh the page and we see I have no user profiles okay so we'll come back to this in a moment but now what I want to do is I want to actually allow the user to create this profile information when they register so right now when I register I see fields for username email first name last name and password but I also want to add in fields for the location and the age so to do that I'm going to create a form first so instead of using this extended user creation form I'll create a new form for the profile so we'll call this let's see profile form or a user profile form because I called this user profile right yeah so user profile form and then it's going to be a model form so model form and then what I want to do is I want to define the metaclass and then the model is going to be equal to and then I'll have to import the model so from that models import user profile so the model is going to be user profile and then the fields are going to be location and age so the two fields that I just created so can I import model form and actually should be forms dot model form and I need to remove this model form from here so let's save that and see if it works now it does okay so I have the model form here for user profile form and now what I want to do is I want to actually use that as a regular form so I'll go back to views and I will import that form that I just created so user profile form just like that and then what I want to do is I'm just going to add on to this part here so I'm going to have a second form and I'll call this profile form and it's going to be user profile form request stop post so you'll see how this kind of follows the same pattern profile form user so I'm going to have two forms in this view instead of one and I'll pass it to the context so profile form just like that okay so now that I have that pass I'm going to add in addition to the regular form being valid so the user form I'm going to say if profile form is valid and now this part is the part that I need to add on to so basically what happens is when I use form not save I'm creating a user so what I'll do is I'll return a user from that form save and then what I want to do is I want to create a new profile using the data from the form so I'll just call this profile and it's going to be profile form dot save but I have to add the user to this profile so I'm going to use commit equals false that way I don't save it to the database right away so this is going to create a profile object but it's not going to save it and it's going to give it to me here that way I can use it so I can say profile dot user is equal to user and this is where the one sub one field comes in so whereas in my models it's going to automatically include it there and the location and the age they're built into the form so they actually get saved when I hit save so now if I just go profile dot save that should save my user so now the last thing I need to do is go in the template and add that form so as long as I add the form inside of the same form tags it's going to get submitted at the same time even though it's a separate form so profile form as P so let's save that no errors and then look at the registration form again so now in addition to seeing all that other information I see location and age so let's create a new user I'll just call this use new user new user at tests comm new user password password 11 let's see password 11 password 11 location will be New York and age will be 25 so create a new account and I see the user name is new user the email is new user at test com so now if I log out and login as an admin user just so I can see the model if I go to user profiles I see the new user profile here so new user and I see all the information here that I'm interested in seeing for this particular user so the user name is new user locations New York and age is 25 and if I change this drop down to something else that will update the user but I don't want to do that because there's really no point in doing it so now what I want to do is just show you that I can view this information so if I go back and log in a side user so login new user and then password 11 I see your user name is new user your email is new user at test com so I also want to add something like you know your location so your location is so user dots and let's look at the model user underscore profile because it's taking the name of it and then location so let's see if this works your location is blank so I think I need to get rid of the space there let's try that again there we go so your location is New York so I'm looking at the user profile for this user and I'm getting the location if I change this to age your is blank your age is 25 so we see that that works there so I'll just add them both so we can see them your location is user user profile dot location and now I see the location for this user in the age so we see how it's pretty easy to add in additional information for a particular user and if you once you add in more information than you would just add on to this model and use whatever feels that applied to your particular use case
Info
Channel: Pretty Printed
Views: 33,834
Rating: undefined out of 5
Keywords: onetoonefield django, user model, one to one field, programming, django, extending user model, python, web development
Id: Tja4I_rgspI
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Mon Jan 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.