Django Custom User Model | Extend User Fields | Django ORM Mastery

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Anna welcome back to the  Django Aura Mastery series you will find a   link to all the tutorials in this playlist  there is a link in the video description   this is the third tutorial in this mini series  where we're focusing in on the user model   what we have seen so far is that if your Django  application has very basic requirements the Django   user model may already accommodate your needs  the approach demonstrated in this tutorial or   introduced in this tutorial the general idea  is that potentially you are looking to extend   the user model and add some additional fields  the default Django model has 11 user Fields the   idea here is that we want to add some additional  Fields now we don't we aren't going to do that by   adding additional Fields directly to the user  table we're going to create a new table we're   going to call this the profile table so the idea  here is that potentially we want to store more   additional information about the user and we're  going to create a new table and that's going to   be a a one-to-one link over to the user table  so we're going to create a new table called   profile We'll add some additional Fields here  and we'll create a one-to-one connection from   the profile table all over to the all over  over to the user table if you would like to   master database interactions with Django then  please do consider looking or purchasing our   Django Forex RM Mastery course it is currently  the highest rated course for Django RM you'll   find a link in the video description that should  always give you the best price for the course   your table your profile table isn't limited  just to two or three fields of course you can   add as many fields as you like depending on the  information that you want to store but the idea   here is that this information this profile  information like the email like last name   Leica the first name field in the user model  the Django user model these are all going to   be optional fields I make the assumption that the  fields that we add in the profile table these are   fields that can be added potentially by the user  once they have already registered or once they   register to your application or to your service  I have broken this tutorial down into separate   steps so that you can choose how far how deep you  want to go into this tutorial we will first start   by creating the new models hooking it up looking  at some of the different dynamics that we need to   incorporate so for example if we are going to use  a one-to-one table then we ought to add an entry   into that new profile table whenever we create a  new user so we need to learn how to handle that   and then we go into the admin site look at how we  can hook up this new model this one to one table   with the admin site and then in addition to that  I'll show you how we can create a new profile form   so that when the user is logged in they  can go ahead and update their new profile   we start this tutorial where we left off in the  previous tutorial so it's exactly the same code   base there is a link in the video description to  this code base so you can go ahead and download   start a new virtual environment and then just  run the requirements file here and that will   get you to this point here there is a setup  guide I suppose in the previous tutorial so if   you are completely new it's well worth seeing the  previous tutorial and going through that process   and then moving back to this tutorial so first  of all let's just go into the user models here   and I think we just remove or just highlight  or comment out the existing code there and we   can go ahead now and create a new person model  right probably what we're going to need to do   is just reset the database so we'll just remove  that database and then in addition to that don't   forget to also remove the migration okay right so  let's build a new model table say class and we'll   call this user profile so we're going to add some  additional fields in our new user profile model   so models dot model as per normal and let's now  create a a new connection so user equals so here   we're going to need to create a one to one field  and that's going to be over to the user model the   Django default user model and then on delete  will be models Dot Cascade so if we were to   delete a user from the user table then we would  also delete the entry in the user profile table so now we can just go ahead and add any  of the other additional fields that you   want to include here I'm just going to make  up two fields and models dot integer field   and then in addition to that in addition  to that let's go for a nickname ecos models   dot Char field and then Max Lent let's just set  this to 100 and what we need to consider now is   that by default don't forget that these fields  are mandatory so if we were to create a new user   now it would mean that we would also need to pass  in this additional information about the age and   nickname so we need to be careful here and make  sure that we set this these fields so that they   can be null or empty so let's say let's say no  equals true blank so there are different rules   here for different fields so go ahead and have a  look at the different field types but I'm going   to set null true blank true to age and let's  just do the same for the character field so no   equals true blank equals true okay so I've just  set here the fact that when we create a new user   when we actually attach a new user to the user  profile we don't need to necessarily also Supply   the age and nickname that can be a blank field to  begin with and then what's the expectation that   the user will then go into their profile and then  add some additional information if you do want to   create new fields and you do want to make them  mandatory it's likely that you're going to want   to take a different approach to this one here this  is really for situations where you don't want to   make many changes to the user model the existing  user model but you know that you might need a few   addition additional Fields maybe this approach  might be appropriate but there are additional   approaches and we will continue this tutorial  series to have a look at those different options   so now we have our new table and Fields  in place let's go ahead and just for   just so we can use this information in different  places let's add a string Thunder method here   apologies now what's going on my fingers  today return there we go again return   self.user Dot and then we return the username just  to make it more usable so we understand what data   is that is we're working with so we return the  username of the particular user whenever we return   objects right so that's the default Dunder string  method so let's go ahead now and actually migrate   this now remember we have deleted the database  and migration so python3 managed apply let's go   ahead and make migrations so you can see that we  have a unexpected keyword blank um yeah because   I'm using Capital apologies I'm not entirely  sure why I did that so let's run that again   migrations there we go so we're going to create a  new user profile as well as set above the default   Django models so let's go in and now migrate so  we migrate all the data so we should have a new   database now so in comparison to the previous  previous example where we created a proxy we   should now have an additional table and that's our  one to one table we just defined so here we have   our user profile and you can see that we have the  age nickname and in addition to that the user ID   so now we created a new table we now need to  actually add some data of course so let's take a   look at the default Behavior now here I'm running  sqlite extension if you're wondering how I access   the database go over to the extensions type in  SQ Lite that should take you to or present a an   extension here called sqlite go ahead and download  that and that just allows you to interact with   your SQL light or sqlite databases and you can see  that I can access the data by right clicking and   showing the table here right so um let's go ahead  and add some data yep right so we'll go ahead and   type in python3 match.pi and then create super  user so even the CPUs even the admin user we   want to potentially attach to some user profile  data so let's go ahead and add a new super user   so just admin no email password password whatever  at this point okay so I've added a new user so if   I go into the user table show table you can see  I've added a new user here this is the user we've   just created now ideally what I want to happen  is that whenever we create a new user I want to   attach them to a profile I want that to be an  entry ready for them to actually then attach or   to add sorry any other additional information into  their profile at this minute if I go into the user   profile table and show the table you can see that  we don't currently have any information stored so   what we need to learn here is a little bit about  signals so Django includes a signal dispatcher and   this essentially allows us to be notified of when  actions occur elsewhere within our application so take example what we've just done there in  the previous task is that we created a new user   so Django will send out signals or we can capture  these signals to determine that has just happened   on a particular model in this case the user model  we added a new user what we can do is we can   capture a signal either pre-save or post save now  this is just one of the two of the signals that   we can utilize there are many others for example  when we delete data or change the data there are   other signals to capture this that information  but here we're going to utilize post save signal   and that's going to give us a notification when  for example we add a new user to the user table   so let's go ahead and start by  using the receiver declarator here um and then we need to Define what notification  or when we want to be notified so for example   here we want to be notified on post save so like  I said there's pre-save and post safe so this is   going to be um utilized after a save has happened  so we're going to capture this information when a   Savers happened and then we also need to then  Define the sender so the sender is going to be   the user obviously the the user model yep so here  we're basically capturing um a signal when the   user has saved new users into the database we're  going to capture the um a signal from Django which   is then going to help us determine that a new user  has been saved and we can now perform additional   actions okay so let's create a new function here  we're going to call this create user profile   because what we want to do is we want to now  whenever a new user has been created to also   now add them to the user profile table all right  so by all means go ahead and have a look at the   have a look at the uh Django documentation for the  additional parameters and attributes for signals   but here we're just going to tap this out so if  if created okay so if a user has been created   then we're going to perform some sort of action  so we're going to take the user profile table   we're going to use the default manager and then  we're going to use create so we're going to create   a new instance or new object and relate that to  the user that has just been created so what we can   do here then is we can say user equals instance  so you can see here what's happening is uh we're   passing in this instance so whenever we have  received a signal the fact that the user has been   created we can gather that data we can collect  that data and pass it on over to our function   here and we can then utilize that information to  then create a new entry in our user profile table so we are going to need to bring in from  Django Dot dispatch let's import receiver   so we can capture that information now  we are going to need a semicolon there   and then we just need to bring in the post  save so from uh uh django.db so this is   to do with the databases models.signals  let's import post safe okay there we go so just to recap whenever we now save a user  in the user table Django is going to send out   a signal and we're now going to be notified of  this signal we're going to receive this signal   and then we're going to go ahead and check to see  if a user has been created and then we use the   instance the user that has been created will use  that data to then populate the user profile table   so let's try this again we'll go ahead and let's  just delete the databases again we'll start from   fresh and we use our previous command so make  migrations we'll then go ahead and migrate and   then we'll go ahead and create a new super  user so I'm just pressing up and down on   the keyboard here bringing up the previous  commands so create a new super user admin   admin admin yes and now let's take a look  at our new database open up the database   we should now see inside the user profile here if  we inspect the table we should now see a new user   so now we have a way of actually populating this  table whenever we create a new user let's now   think about actually allowing them to maybe update  that information so as I mentioned at the start of   this tutorial I was going to just quickly show you  how to potentially add a a method of updating this   data for example in the Django admin and how  you might create a simple form to capture this   information when a user logs in so maybe there's  a a new page that you create in your application   called user profile they click on that link and  that takes into a page and they can then update   their user information so apologies I didn't  give you much information here so you can skip   this little section let me just break this down  a little bit more um I feel as I need to just   iterate over this again I tried to just tell you  in layman terms enough information for you to be   able to replicate this for additional models that  you might want to create but here essentially what   we have is post save and I will leave a link in  the video description to the documentation so   the post save signal sends a number of different  arguments so here we have the sender which is the   model class so that's the sender of this signal  from the user table so we're going to capture   post saves if you like signals from the user table  so here we have a few different options here so we   have sender for example so the sender here is the  model class okay so that's the the class model if   we want to utilize that we have the instance and  that's the actual instance um that was being saved   so we can grab that instance you can see what  we're doing here when we create a new entry in   our table in our new model that we're using that  instance to help populate our table and make   the connection because all we need to do here if  you're not familiar to one to one is we just need   to specify the ID of the new user from the user  model so when we create a new user in the user   model that's going to be provided a primary key an  ID and all we're doing is we're just matching that   saving that here so we can connect this record  that we're building in the user profile over to   the user model okay so we also have created  this is just a Boolean field if I remember   um true return true if the actual uh if the  actual record was created and fought if it   wasn't so here you can see if created so if this  record wasn't created if the user um wasn't saved   correctly then this is going to be false so we  don't then go ahead and update our new table so   this will return true or false depending whether  the user was successfully added to the user table so in order to set up our admin what we're  going to need to do is go into our admin   here we're going to need to potentially use an  inline so if you haven't used an inline before   now if you remember when we go into the if you've  used the Django site before typically when we use   the Django admin site it will present to us a an  interface a form which represents data that we can   save in partic in an individual model now here  in this case we have two models so we're going   to use an inline and basically to stack up the the  forms so that when we enter the user form at the   bottom we're fine we can now utilize the profile  form so whenever we save the user form it will   also update and save the data in the profile form  so let's give this a go so from user dot models   let's import the user profile so we're  going to need that table we're going to   need to use a table as well so let's just  bring that in so django.com trip dot auth   dot models this is obviously just one way of of  accessing the auth models let's import the user   and then in addition to that  let's go ahead and import   um the author user admin so we are going to  unregister the old admin and then register   our new admin to update it with the new settings  that we defined so let's go from Django .com trip dot or dot admin inside here we're  going to put import the user admin   um yeah let's uh output the user admin as auth  user admin okay right so let's go ahead and   create a new class so class user Pro profile uh  inline so we're going to create a new inline and   basically what we're doing here is we're setting  up this inline which is basically capturing all   the information about our user profile table  and then we can then utilize that and then   add it if you like to the the normal user admin  form so let's go for the user admin admin Dot   stacked inline so there's a few  different options here I'm going   to use a stacked in line for no particular  reason really so model equals user profile so that's the model um can delete so we can set  different options for example can delete equals   um false so let's not allow them to maybe  delete data for example let's remove that option   um okay there we go so now we have our inline  let's go ahead and just have a look at the   default options to begin with so we have  we have already created a new admin user   so what we need to do at this point is run  our service run server take a look over here so just go into the admin should just be admin  admin that's that's what we just created earlier   so let's go into users this is the default  user setting if go into admin here you can   see at the moment all I have access to is the  the traditional the normal uh Fields additional   Fields about personal information first name last  name email so no other additionalities so let's   go ahead now and actually attach our form here  so it appears at the bottom here so we can add   that additional profile information here in the  admin site so first of all let's create a new   class I call this accounts user admin auth user  admin okay um and then we set up some inlines Egos and that's going to be the user  profile inline so you can see here we've   set up this form if you like and we're  now going to attach it to the accounts   user admin and that's essentially what  we're referring to here this page here okay right so we now have that in place so what  we want to do at this point is we just want to yeah let's just try that see what happens okay so  we refresh here and you can see nothing happens at   this point okay so what we need to do is in order  to get this working is that we need to say admin   site dot unregister so we need to unregister user  and now we want to apply our our customization so   we say admin Dot site Dot and then register  so we're now going to register our user   and then account s admin user okay so now we have  that back in place let's go for a refresh and you   can now see at the bottom here we have the user  profile information from our user profile table   all good right so let's go ahead and add some data  press save and let's take a look see if that has   saved looks like it has saved now let's go into  the our table here let's open up a database have a   look at our data and you can see that our new data  has been saved so here's the problem with this   approach if I go back into my user here and I add  a new user notice that the user profile profile   information is also being attached to adding a  user so let's just go for test I just create a   new user and we can see the error that occurs here  so let's add in some information press save and   you can see we get this Integrity Arrow unique  constraint failed user profile user ID so it   causes an error by default and although we could  probably overcome this and actually have this   attached to our data that isn't the approach that  we're trying to create here and that's something   that we're doing a later tutorial where we add  additional information potentially when we add in   new users so this isn't something that we desire  so how can we get rid of this well we can kind of   go back a step here and think about well these are  different views this view here is different from   for example the add user View this is a different  form completely a different area a different piece   of code so we can separate that or we can override  this Behavior utilizing the ad view overriding the   ad View and the change view remember this is the  add view right here I say remember this this is   the ad view if you're not familiar with this and  then this is going to be the the change view so we   can override these views and so that the profile  data will appear down here as well as allowing the   user to type in or update but we can then remove  it from the add view so let's go ahead and do that   so back in our code here let's make a few  different changes I just drop that down   so what we're going to need to do here then is  just expand the accounts user admin um Let's uh   just Define first of all the add the add View so  take in self and maybe any kind of arcs and quarks okay right so self dot inline so we're going to  set the inline for the add view inline and that's   going to equal nothing so what we want to do here  is when we go to the add view we want to make sure   that there are no inlines so we just need to  override and return so return super user admin   self and then dot add View arcs clocks okay so what we've  done here then is we've simply   just override the existing Behavior  we've removed any in lines and then   we've basically just returned that back  and it now should show as per normal right   um just wondering what the problem is  here so user admin is not defined um Okay so I've named it accounts  user having that's why   accounts use rabbin um that makes sense and then  I need a nest there and there we go right so that   works out nicely there so let's go ahead now and  just try this out so if I refresh you can now see   that form has now been removed from this View so  we do want to now add it to the other view the   change view so let's just create a new function  here now I'm just going to copy and paste this   and this is going to be the change View  um here we are going to Define our inline and in addition to that um let's go ahead and just make sure  we Define our accounts user admin   yep that's in place um yeah there we go I think  that's uh pretty much all we need to do there   so let's go back in let's go to our users go  to our admin okay unexpected keyword object ID okay so notice that we're gonna need a plural in   lines so make sure that's in place  hopefully that will fix it no okay okay I've not changed this yet so it's the change  view apologies right so change View uh refresh go   down to the bottom it's still there should work  okay yep there we go so when we had a new user you   can see it's been removed and then we go into the  admin here the the change view you can see it's   now available so I do apologize if you wanted some  more information and I'm trying to build a a clear   division here between what I'm trying to show in  this tutorial which is really predominantly the   tables and how to build the tables and related  to the RM rather than templating and styling   Etc so there's obviously a lot of information  here that potentially have lots of questions and   what are args and quarks maybe there is a tutorial  on that to help you get started here to add view   change view you know what else is available and  so on but hopefully you can get the general flow   of what is happening here and that's really the  principle here the rest of this information we   can learn from additional tutorials or potentially  the Google The Google the the Django documentation   so now we have the admin in place let me just  take you through the process of building a form so   we're going to make the assumption that the user  needs to be logged in and when they are logged   in they can access a new page called profile for  example so we're going to build a new template a   profile template we go ahead and build a form  and we showcase how we can then present that   form so that the user can update their profile  on the webpage as long as they're logged in   let's sort out the URLs first so we're going to  pass a set of URLs or we're going to create a new   set of URLs sorry in the user space so let's go  path let's set up a A New Path uh so we go include   and then we want to include the user URLs so we're  going to create a new file in a second URLs and   then we're going to give a namespace provider  namespace and that's going to help us easily   access maybe some URLs if we want to for example  redirect the user so we've got the namespace user   here and now we've got that in place let's go  ahead and go into the the user app here let's   create a new file urls.pi and we're going to  Now set up this somehow okay so we're going to   need a Django oh we're gonna need a from Django  dot urls and we're going to import path and then I'll just zoom in a bit and then we're  also going to need to have it from Dot   let's import views okay so we're going to give  this an app name we have got a namespace so it's   going to be set to the namespace name user that  we defined in the previous URLs so URL patterns   let's set out our patterns here we're going to  have a path A New Path so we're going to set   out a new profile page that's what we're going  to be building and that's going to be connected   to a new view so views.update profile so we're  going to make a new view called update profile   profile I've got product on my mind and then we're  going to give this a name so we can access this   maybe for reverse or redirect URL profile there we  go so so there we have our new URL right so next   up we need to attach this to our view so let's  now go into the views we are going to be building   a view called update profile so go ahead and do  that so update profile we're taking the request so here we're going to be working with with  two forms remember we're going to need a form   to collect and validate data in the user  table and then we're also going to need to   collect and validate that data in the user  profile so here we're going to be utilizing   two forms so let's first of all just build the  forms now we could create a form file for this   but I'm just going to do it right here so user  form we're going to get forms dot model form and here we're going to set our class  meta so we're going to define the model the model is going to be in this case the  user model and then the fields that we   want to use the fields that we want to use  so let's define our first name lowercase   so these are the fields that we want to show to  the user so that they can fill in in our form last name okay so that's the first form let's now  create our second form we're going to put these   forms together so it's going to look seamless  and so there isn't two forms shown to the user   as such it will look like just one form so we've  got the user form so again forms dot model form and same process so grab our meta the model that's going to be the user profile and  then we need the fields that's going to be the age and the second one was nickname Okay  so we've now defined our two forms   let's just go ahead and add in so I've got  the yeah what's this anyway okay so from   I thought we was going to be able to do that  automatically from Django import forms so let's   bring in forms what else we're going to need here  we're going to need some tables some models so   let's do that from dot models let's go ahead and  import the user profile and then in addition to   that we are going to need the user so from Django  Dot contrib dot fourth dot models let's import in import user thank you um right so now we have  that in place let's go ahead now and focus in   on our view now we do want to make sure that the  user has logged in so we don't want them to access   the user profile of course unless they're logged  in so a few different ways to do this but we're   going to use the the login required decorator  here we're using functions and function based   views and in addition to that we want to make sure  that whenever we save the form related to the user   in the user table if we update that we also need  to make sure that the user profile form was also   updated what we don't want to do is get into a  situation where one form is updating correctly   and one form isn't so this is a good example  potentially where we can use transaction atomic   if you're not familiar with transaction Atomic  then there are videos on this two on this channel   which will help you out there but essentially  this will allow us or we'll ensure that if   there's any problems at all saving to the the  two models if one model has a problem then the   other model won't get saved so this ensures  that both data both forms successfully save   to the database in order to actually apply  that data to the database so data won't get   saved to the database if one of the forms fails  so we're going to use transaction Atomic there   so we're going to need to bring all this into  our into our project here so from the Django DB we're going to import transaction and then for  the login required from django.com trip auth or decorators and let's import  the login required yeah okay   always press that wrong button on the keyboard  right so that's what we've got up here then in   the Imports so let's go back down now and  finish this off right so the first thing   that we need to do is check to make sure  check to see if for example the request so that's the user request so has the user gone  to that page or has the user actually pressed   the button to submit data that's what we're  trying to capture here so we'll check to see   if the request method equals post or not so if  the user has gone to our use update profile page   by just navigating the link then the request  method will not be post it would just be get if for example the user has accessed our profile  page via the button so if they have filled in some   profile data and press the button they would have  posted some data so therefore the request method   will be post right so let's capture that so if  they do pay some data we're going to do something   right and that's where we're going to save the  data if they don't post if they just navigate to   our page we want to sort out something so we're  going to create a let's set up the user form so   the user form is going to be user form that's  our user form let's just grab an an instance of   that and what instance do we need well we need  to show information related to that particular   user so we can grab the user information from  the request if the user is logged in let me just   move this across so what we've done here is  we've essentially just populated the user form yep the user form we populated the user form why  that's we're going to sprout that incorrectly   haven't I uh based upon the user that's logged  in that's what we've done there essentially so   let's do the same thing for the user profile  so user profile form let's say the user profile   four and then we want to populate that so  instantly cuts request dot user so get the   user request information and then we need  to use a profile data so it's going to be   user you can just put user profile so that's how  we're going to grab the user profile information   um based upon the the current user that's  utilizing or accessing their user profile   right so now we have that in place let's go ahead  and just test this out by returning our render   uh and what we need to do is pass back our  request we're going to need to take it make a   template so we'll call this profile.html and  then we're going to need to pass back some   data to our template so let's go for user  form that's going to be the user form data um let's go for P form that's going to be the the user profile form okay so we pass this data  back to our template so we can   utilize it and create our form and  show that the current user's data I'm going to stop doing that right so that's  the final code right there for that so now we   can go ahead and test this out but before we do  that of course we are going to need a template   so I'll just remove that so let's build a new  template in the user application here let's   create a new template folder and we're going to  need to create a new file called profile.html   cool so let's just grab a template  so let's go to bootstrap here   um let's go to documentation let us grab a default  bootstrap setup this second option here I'm just   going to copy this I'm going to paste that  right here I don't need that well let's just   check to make this sure this works to begin with  actually so that is now in place so we now have a   URL we now have a view we now have a template  so the question is will this actually work   so let's um no module named user.urls so that  already looks like a a problem that we have   um URLs is named correctly so we'll sort out in a second let's just   um try this again okay so let's  go back into our application um eight thousand okay so it looks like we have  a profile link ready profile there we go so   template does not exist so at the moment we  can't find the template so profile.html so   I've named it correctly it is in a template  folder but if I just rename that to templates   is probably going to be more effective  so Django is looking for the default   name of templates so that was my fault so if  I refresh now we can see that we still have a   an error so profile.html just double check in  the name looks absolutely fine so let's just stop   and restart the server and go for a refresh  it looks like it now works so our template   is now working which is great so now we can go  ahead and edit our template to add our new form   right so I'll just bring this down a little bit  so we can see what we're doing so let's go ahead   and build our new form so for this we're going to  create a new set of any block a new set of dips I don't have this setup automatically online  so div and then here we're using bootstraps of   class equals uh container and then we're going  to create some padding at the top maybe five   and then we just need to place this we need to  put this we don't need to but we're going to   put this kind of Center on the page so a new block  here so class equals uh we're going to use D Flex   I'm just going to put this in the middle  basically so I'm just going to just   ify content um Center and then I can go  ahead and put my form in place right okay okay so my form uh that's going to be   that is going to be method so when I send the  method I send it's going to be post for example   um we're going to need to make sure that we've got  our CS RF token in place and we are going to then   need to utilize u4 remember this is the context  reference you form as P so we're going to serve   the the form as piece the the uh God can't  think of the word the form inputs oh okay so P   underscore form dot as p right so we are going to  need a button as well so let's create a new button type equals and let's go for submit and then class  this is just all bootstrap stuff pnt BNT primary   it's be for styling so it's going to be a nice  blue color and then we're going to have a name   in the middle of the button we've just  finished off the button okay so that's   our button in place so hopefully when we  go back and refresh we can now see this so that wasn't quite what I was looking for so I  just noticed a typo here D Flex so I refresh again   and there we go so that's what I was looking for  so you can see here I am logged in as the admin   and the admin we have actually added some data  so you can see that we've added this data here   so let's just change our nickname to ads uh and  then age 22 and we go ahead and just change the   information here as well in the default so this  is the admin admin last name admin and so on so we   save that and you can see what's happening in our  form if I refresh we're just picking up the the   the information that's currently being stored so  let's go ahead and change this to something else   so age 23 for example with an admin of course  admin one press submit and you can see here   at the moment we haven't set up this this feature  but we are now going to need to set up the feature   so we can actually store information back into  the database so let's go ahead and do that now   right so we have everything in place  in terms of presenting the default   form to the user with our else here now obviously  what we've done there is we press the button we   sent a post request and we passed so now what  we need to do is fill this in and actually save   that data to the database so let's populate the  data to begin with so I'm going to copy this   up to the top here so this is how we populated  the form previously so this time what we want   to do is we want to get the data from the post  so user form we're going to say request.post   and then get the instance so essentially  what we're doing here we're storing the data   that the user has saved to the new form what  they've actually typed in so we now have our   new data so all we need to do now of course  is to to store it right so let's move this into here just to line this up so the first thing  we do is we check to see if the user has actually   pressed the button and sent a post request we  then grab that information and ready it so that   we can store it in the databases right so next  up then we will need to check or just validate   the data so user form we use is is valid so  we're just going to check the data the user   form data and also the user profile for data  so we're going to check to see if that is valid okay so if we're happy with that we can  then go ahead and say user form dot save   and let me go ahead and also  the user profile form dot save okay so now we've saved the data we've checked  the data and now we've gone ahead and validated   the data and we've gone ahead and saved  it um let's now just return the user back   so we say return redirect and then what we're  going to do here is use the context so user   and then we're going to use profile you saw  earlier we created a URL structure where we   set out the namespace and we gave the app name  and then also we then named the URL so what   we're doing here is we're just grabbing that  URL via the namespace and name of the URL and   then we can then return that URL and return  the user to that URL so that's the redirect   so let's go in and change those shortcuts I don't  think I've added I must have a render here so   let's uh do that so render and redirect okay so  that's the redirect sorted um so what we could   also do here of course is um you could capture  any errors so you could have an else here and   capture any errors I'm not going to put that here  in this code but that is something to think about   so if the user has set press the button would grab  the data and then we save it to a database we're   using transaction Atomic so both um both tables  need to be saved correctly before we commit to the   database and then we return the user if the user  hasn't pressed a button and just navigated to the   page we show them that their current data and then  return that data to the form so let's give this a   go let's go back let's just uh submit again and  you can see this time I submitted doesn't look   like there's a problem so let's go back into the  admin and you can see now that has been changed   and then that has also been changed so we clearly  now have a form that when users log in the data   will then be populated here based upon the current  user that's logged in and this form will now allow   them to save data in the two forms the one-to-one  table that we created and the existing user model   thank you very much for listening there will be  more tutorials we will look at furthering our   knowledge of the user model moving into creating  now more bespoke and customized user models   don't forget to check out us on all  the socials follow us and so on and   if you are thinking about learning more  about the Django RM then please consider   purchasing our course on the Django RM  there is a link in the video description   thank you very much for listening  and I'll see you in the next tutorial
Info
Channel: Very Academy
Views: 9,984
Rating: undefined out of 5
Keywords: custom user mode, django custom user model, django user model, django ORM, django databases, django models, django orm, orm in django, django orm example, django orm in, orm in python django, orm model django, django database, django db
Id: NLHmadrP8Y4
Channel Id: undefined
Length: 50min 29sec (3029 seconds)
Published: Tue Jul 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.