Python Django - Build Inline Formsets (with CBV's) Example 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a django inline form set example so before we start i'm going to tell you it's going to be a class based view example in this example we're going to be utilizing just two tables with one foreign key now this is just one of two tutorials i'm going to build for inline form set so here we're going to start with the more simplistic two tables with one foreign key and then we'll move on to the three tables with one phone key in the next tutorial in this example then we're going to build a very small scaled application in django whereby we're going to utilize our two tables with one foreign key to build a form that's going to allow us to add edit update and delete all from one form so just finally to point out and to give you more information about what's expected here in this example like i said we're going to be utilizing class based views so template view list view create view detail view and form view and also bring in the single object mix in and then we go ahead and think about some overrides for the form view so we build our own functions for get post and the get form so let me just take you through a preview of what it is that we're going to build so there aren't any wonderful graphics here this is just a simple setup which you can expand so we've got two options to begin with all authors and add an author so let's just go ahead and add an author let's just call this uh name one so we can add authors that might be adding a new recipe that might be adding a new book in a bigger store that might be adding a new invoice or a new receipt or something anything that has a foreign key parent child relationship set up we can use that here so let's add a new author so now we have a new author and we have a message also has been added so i'll show you how to do that and we have the author name here so now what we can do is edit the author books so this is where we're going to need our inline in order to add all this functionality here because this is all going to be added automatically what i can do here is add books and you can expand upon these um fields this field set easily so i'm able to add books update the collection now i can see all the books are associated to this author here name one so now i've got that i can then go in and just edit them i can delete that one i can update this one to book 22 and then potentially i can add a new one so i can perform multiple operations at the same time and then just go ahead and update the collection and there we go so all the time we're associating our data to this particular author this is all the books this could be like i said anything that you might have and this is all using class based views and that's going to take a lot of the back end work out for you to kind of add all this functionality that you would have to do manually normally utilizing functions or function based views so in addition to that um that's really what we're building up to and we can have a look at the whole collection all the authors and then drill down to each author and see what books are associated to them and again like i said that might be recipes that might be invoices and that might be quite trying to create a purchase and so on in this tutorial we're going to be utilizing two tables now here i'm using author and book table you could replace this with category and book maybe a collection and book it could be invoice and invoice items for example so this could be a whole host of different connections you might have between or situations you might have with two tables with a foreign key so here in this example we're going to have two tables an author table and a book table which is going to have a foreign key to the author table so essentially here we're building a one to many so one author may have many books and our aim really is to display a form that will show the author information and all the books associated to that author so a classic situation we may have a form that allows us to update add edit data in the author table and then we might build another form that allows us to do the same thing again with the book table so an alternative way of working is utilizing an inline form set so as django documentation suggests these simplify the case of working with related objects via a foreign key so because the author table in the book table has have a foreign key between the tables it means potentially we can work with the data in the author table and the book table at the same time and this is all simplified for us utilizing the inline form set feature so now we can potentially build a form that can grab data from both the author and book table and that will allow us to perform actions within one form for example view the data within the form edit the data in the form delete and also then add new data from that single form so just focus in there on this form and the ability for this form to view edit delete and add data to these tables from the one form this is going to be further simplified by in this example using class based forms so before we start this is what we're going to build up towards we're going to try and build a form that's going to allow us to access all the books about a particular author and within this form it's going to show us all of the books that are associated to that author we're going to be able to edit those books in place and then also and that will mean we can either edit the text or delete them so we can choose that or at the bottom of the form we can then add some more data to or add a new book in this case to the author so that we're going to be able to do that and those multiple tasks all at once in that one form so when we press submit that's going to update delete edit that data and save it in the corresponding tables so i've gone ahead and created a new folder for my project and i'm here in visual studio code so let's go ahead now and just create a new virtual machine and then of course we just need to activate that machine so um you can skip ahead all this if you like maybe i'll save the project halfway through before we get into the kind of juicy bits of the code and then if you're familiar with setting up a project you can just skip to that section so let's now uh go ahead and just move into there so i will do this step by step and i have some feedback from the community that some would prefer me to type everything out so i try and mix it up a little bit here and type some things out rather than just copy and paste them in as i prefer to do because i can easily then explain them i find it very hard to type and talk at the same time okay so we can activate the project so let's uh uh django uh we're gonna pip install django so once we've done that we'll go ahead and create a new project so let's go ahead and django admin uh there's a start project sorry um so we're going to just call this has been all core space dot so don't forget the dot there and then we're now going to create a a new project then so the start sorry app um we'll just call this we call this books so now we've made that of course we're just going to go into the core and settings and add this to our app books there we go so we've added the project so we're now going to go ahead and create our simple tables for this example so inside of our models here we're going to need two tables so i'll make the assumption that you know how to make a simple model so let's just go ahead and see this so we're going to bring in models so we can build some models we're going to need to create a reverse later um so we'll bring that in for now that's going to be for a later point where we be able to get absolute url points so that we can reverse the url and then we're going to create a class here called author uh so we're gonna have two classes that's just gonna have a name so i'm gonna set this up here with the null false blank false max length of two five five so we're gonna have to have a name here say blank false and null false so we're forcing the data to be entered here in this field so that's pretty much all we need you can add some more if you like um we're just going to set this up in a nice simple way so we're just going to return the name of the author by default and done the string method and then we're going to need a a new table here for book so again i'm just going to add a title uh null false blank false and so i'm just folcing fixing it again so that we have to enter something here in the title and then we're going to need to build then our foreign key so author models dot foreign key and then the name of the table that we're building a connection to so remember this is going to be a many to one or one to many so one author will have many books um and there'll be many books associated with one author so uh no false again blank false again so there has to be something here in this author there has to be anyway because we're building a foreign key so we can't build a book unless there is actually a foreign key so there has to be an author set in our database in our sorry in our author table and that's an important thing to understand here if you're not familiar with databases what a foreign key is otherwise this is a little bit lost on you potentially what is happening here so a foreign key we're building a link between this table and the author table via the foreign key so when we make a new author that's going to have a name but remember django also adds the id so this id is going to be unique primary key a unique field that's going to associate the author's name in this database so what we can then do is use that id to uniquely identify that piece of data or that author in the database so that we can then add books individual books to that particular author so what we end up with is the title of the book and then the id of the author that's set here in the author table therefore building a link between this book here and the author and that's what we're going to use that connection is what we're going to use in the inline form to gather and generate data and enable us to select data about specific authors what books the author has in the database so now we've got our model in place let's go ahead now and migrate i think we're ready now to migrate so um [Music] make migrations so that's going to prepare the data of course and then we migrate there we go okay so we should be able to start our server now so it should work uh looks like it's okay so let's now go ahead and actually build this up so we're gonna need a home page of sorts um so let's go ahead and build the first page so we'll start with the urls and so let's just create a new file here called urls.pi and then we just need to go into our core urls of course and just remove all this stuff here and then we're going to need to bring in the include and then that's going to allow us then to extend the paths that are available here so we've now got path and then we want to create a home page so it's just going to be blank and then the comma and then we we need to include right so what we're going to do here is now is include the urls that we're going to set up in our let me just close this in our books application so let's go ahead and do that so we just first of all need to just define the the app books and then dot url so go into the books app and then find the urls and i'm going to give that a namespace of books so that's going to allow us to easily access the urls in this namespace so to speak equals go he calls uh books okay so there we go that makes the link then and again remember we're just using the default root directory of our server passing that across here in the urls so in our books urls we can now kind of start to build this up so we're going to need the well we can just copy everything across but let's just try this out from django.urls we're going to import just path so we just need that and then we're going to need our url pattern that's going to equal a new path so we want to pass our home page so let's just build that so again it's just going to be this root directory and then we now want to connect this up to our view so the first view that we're going to build is going to be called home uh let's just call it home view now remember we're using class based views here so we're going to need to name this with capitals and then it's going to be as view and then we can give this a name name equals and we'll just call this home okay so for mine the default doubles here so if you're wondering i swear my keyboard keeps changing okay oh okay so this is our books right so also going to need here an app name equals that's gonna echo book books i think there's books have a look here so books is a namespace and then books is now the app name so we can get rid of the core now so we're done with that and we can double check if there's any problems of course um by going in here so we've not defined the view of course that's okay so let's get rid of that core url so this is our urls here then in the books so now we're ready to go over to the views and let's just uh build up a view here for our first uh of course we're going to need to import the views so we might as well do that haven't we so from dot import views so now we can go into our views here and we can now start to build our view so the first view we're going to build is obviously the home view now i'm not going to mess about here i'm just going to paste this in so we're going to build a class class based view here it's going to be called home view and we're going to be utilizing the template view from the generic views so we're going to import that in so let's go ahead and do that so if you have seen the rest of the class place view series you've seen that we've already started utilizing and i've given you an overview of these class-based views the default generic views so uh in short then these these views here we're bringing in an existing view with all the functionality that this view provides so the template view then if you have a look at the documentation here um it's just a renders a given template with a context containing parameters captured in the url so this is a simple view that's going to automatically render a given template with any context of any data that we send across so here you can see all we're doing is we're setting up in this class base view the template name which is going to be home so now then in our books let's just set up our templates so we need a tem we're going to use the in-app template so templates templates folder and then inside of here we're going to then add our template so new file home.html and then let's just call that home and then we're going to need a base as well so let's just create a new file here and call this base.html so we're just going to extend from that based in our system so the base is going to be a fairly simple setup offset sort out in a second but let's just go back to our view here so that's it all that we need to do in this view there's nothing else here this is going to show the home page and just to confirm that so we've set out our url for the home page we've created our view which is just going to be showing our template and now we can go into to sort out our templates so if this isn't familiar i'm going to whiz through this so first so we're going to need a dot type because we're using html5 this is on our base template we're going to be building and extending from so we're going to need our html tags i've set the languages en us um we can change that to whatever you like of course and it's not a mandatory field so that's going to be taken from your browser anyway um from the operating system the default language so we've got head here so this is the next set of tags we need head inside of our head i've got some the character set utf-8 which is the default in the browser probably don't need to define that and then we've got the metadata here we're using bootstrap so name viewport content width etc so these are just bootstrap settings or browser settings that allows us to scale the page at certain resolutions etc it just just controls the scale of the page and then we're going to bring in the bootstrap css so this is uh the bootstrap css just go over to the bootstrap webpage go to docs and have a look at the the cdm for it here i'm using 5.0 and then we're add the title so i'm adding a block here for the title because this is going to be overridden on other pages so a block allows me to kind of override this being that i'm going to import this or utilize this base class within all the templates um so i've got my block title for that and the default is going to be called homepage and anything else i override will be overridden anything i include on other pages will override the text here so that's my title then i've got my body tags and then i'm going to put everything within the main tags and then go ahead and create my container fluid from bootstrap so a full width container and then i'm going to create this um flex container here where we're going to align the items in the center and then give it a padding at the top and bottom of five so that's just a bootstrap class for padding at the top and the bottom py and the y-axis so we've got that so we're going to line this kind of these boxes in the middle here and you can see what we're going to do now is we're going to a small scale we're going to show the column or this element in the full width and then as we go further up in resolution we're going to scale down this the elements inside of this element here and then you can see i'm using mx auto so that should help center everything so we've then got the content block so you can see that every page is going to be wrapped up with this so instead of me putting this on every single page i only need to write this once and this is the block content so that's going to be the content of the individual pages that i'm going to be building so remember we're going to extend from this page this is the base page we're going to extend from on other files so now we can go into the the home here now on the home what we're going to need is a button so oh actually first of all what we're going to do is just extend from the base so we're saying get the base template and now we want to drop some stuff in that template so what do we want to override on that base template well we obviously want to override the name so although it's the same in this case let's just do it anyway so we've got now the name and then now we want to sort out our block content so the information that's going to go into inside of that base block content area so let's just open that up uh any blocks that opens obviously it's going to need to be closed uh pretty sure i'm just going to paste in here because i'm just thinking about what i can tell you um so now you've got our block open and close now we can add something so um all we're going to do here is going to be really simple and we're not going to be winning any awards here for design so we're going to need um three things we're going to need two links so one title and two links so the first link is going to be a link to the actual um all the collections or sorry um all the so we're using authors here so that's going to be a link to all the authors and then the second one is we're going to create a new author so there's going to be two links so here i'm using bootstrap button and i'm defining the button as a link and then the roller button i'm going to change my links up in a little bit when i actually build them so here i'm creating some classes so button class because i'm using bootstrap and then the color of the button class and then i'm going to give it a width of 100 and then i'm going to have a margin at the bottom of 2 so i can make a little break or gap between these two buttons and there we go so we've got the second button is going to be the add an author button okay so we've got our two buttons now so let's go ahead and have a look at our new design there we go so this is our it's called bookstore collections um so let's actually change that nothing else okay bookstore collections would be okay wait so um that's now our homepage sorted so now we have an entry point into our system let's do this first let's create a page where we can show all of the authors right so let's just go back so we're going to create just follow the process let me put this in some sort of order we don't need the model so urls views and then templates so we're going to build a new url now so let's go ahead and do that now we're going to be utilizing a different class based views because we want to list out items so we're going to call this authors slash and then it's not going to be home view it's going to be called authors view and then we change the name to author um authors okay so now we've got our path in place uh we can now utilize our app name here let's just open up our our template our home template now we've got that in place we can now go ahead and we can define our we can define our url so let's go ahead and do that so remember we're using this uh namespace to do that so we're going to need the url and then we're going to say we're going to need the books so go into the books app and then inside of our books app you're then going to find the url for um in this case it's going to be called authors uh so let's go in our euro so we've called it we've called it authors so we've said the app name books and then authors so that should take us now to [Music] that page so let's go back into our views now so now we've set up the url and also the link on the home page and let's go now into our views and set out our view so we're going to want to show data so we're going to need to from dot models we're going to need to grab our models and import our table so the table we want to import obviously is obviously author because we want to show all the data from the author so now what we want to do is use a list view so let's go ahead and bring in a list view so this is a another class based view that allows us to list items so here we're just going to give the class the parameters that it needs to actually list items and it's just going to do the rest for us so that's essentially what's happening here so we're abstracting a lot of the code that we don't need to design within these predefined views or predefined classes sorry and that we can bring in from the django backend so let's uh go now and bring or build a new class um so this is going to be called auth author authors list view and then let's bring in the list view for this so here we just need to specify two things and that's going to be the model which is going to be author and then we also then need to specify the template so here you can see we've got template engine name or name suffix so we want the name and that's then going to equal whatever our template name is going to be so this is going to be called the author list dot html okay so now we have done that let's go in and make our template so head back in then to our templates again we're using templates within our app um so let's just create a new file it's called author list there we go so now we just need to build from this so again we're just going to extend from the the base as panel so extend from the base and then now we need to just set out the the title so we can call that authors maybe or all authors so that's the title and then we're going to need our content so we can set out our our content okay so what we wanted to do here then is we just basically just want to return now what's happened here in our view is behind the scenes this list view class has found the model grabbed all the data and now it's going to pass it over to our template so if you're new to class based views you can see that there's no return here that's all done by the the list view class and the other subclasses that is connected to so let's go back into our html and have a look at how to extract that now from our class place view so let's start off then the content with a title so this might be a good time to just test this out to make sure this works now i realize that it's not going to work because i've made a few mistakes here so just going back to the urls i can see that i haven't used the right word there so from dot input views i also notice i've not used commas here either so i'm going to need one i'll just put one here anyway because i know that i'm going to create some new lines later so that needs doing let's just check to see if there's any other problems um okay so you can see that i've missed author's list view from the path here so that's the name of our view authors or it should be really author list view so author's list view is right here so let's just change that to author list okay so that looks like it's working now also i was showing the wrong system earlier and when i was opening up the when i was opening up the project uh so we've got a no reverse match here author is not found it's not a valid view function name okay so let's just go back very quickly then um so authors okay let's just get rid of that space there there we go okay so our project is up and running uh so let's go to all authors so we've now got our template in place now we need to loop through all the authors and display them so before we go ahead and do that there isn't going to be anything to loop out of course so because we don't have any so let's just go into our project here and uh let's just create a super user so normal procedure admin no email admin and an admin and then why yes okay and then we can go ahead and just start the project again so let's just go into our database and add something now before we do that obviously we need to go across to our books let's go into the admin area now of course what we're going to need to do here is obviously register one of our one of our tables right one of our models so let's go ahead and do that so from dot models let's just bring in import our model which is called author so and then let's just use admin admin.site dot register and then that's going to be the author okay so that's our author set let's go into our admin area so we've now got authors here so let's just add one manually very quickly um save that so we've got an author now so uh we want to now loop through those offers and display them of course so let's go ahead and do that okay so what's happening here then is that we are passing data across although it doesn't look like if you're not familiar with class based views we are actually passing across the data the context to the actual template name here and that's important to understand with this list view now we need to grab that data now what we can do here if we wanted to was to add a a context a context object name and that's then going to be the uh the name for the context the data that we're passing across and then we can use that obviously to understand where the data is on our template and use that to output the data sorry so that's a kind of reference point to the data that we're sending across to this template now we don't need to do that because we know that we're using author here and we're creating the list then what how we can grab that data is using author underscore list so what we're going to do here is create an if statement and you can see i've built this if statement here so if the data if there is data here [Music] in this context and this is the name of the context of the data that we've grabbed from the author table um all the offers um if there is that data then we're going to do something else basically there is no author to display of course so we're going to use an if out statement here so if there isn't any data we're going to kind of just output there is no author to display now what we can do if there is data um then we're going to create a an unordered list using bootstrap so list group and then we create some items so what we need to do here is use a loop so for each item that is found in the order list context so for each item there is in the database we're going to loop out that information so we put that information that individual piece that we're looping through at that point in time into the author variable here say and then now we can output a new list item i'm going to give this a list group item which is again bootstrap class and then i'm just going to output this out so i'm going to grab the data now from author about that individual item that's being looped through and so that's going to be just the name that's all i've got in my database of course and then there we go so i'm going to need a link for this so i'm going to need to build a reverse here so i can get the actual link for this author so we do that shortly um but let's just uh let's just go ahead and save that and let's have a go at this so this should work now so going back into my home page let's go to all authors so i do have one author remember i did add one so let's just add author two just to show this is working and there we go so we've now got all authors on the page so now i'm going to create a link here at the bottom i've created a p tag inside of here i'm going to create a link so this link is going to take me to this link is going to take me to the home so it all says home now is that the right thing now it's books isn't it okay so books home uh so the role is a button class uh button secondary so it's gonna be a great button with a width of a hundred uh so that's going to have a cancel link so let's have a look at that oh that's not working home not found okay let's go back in so home urls so in books we've got our books and we didn't call that home so let's call that home that should now work there we go and then cancel yeah now that takes us home so now we've got a way of displaying all the authors in our system so now we want to be able to add a new author right so we want to build an author so then we can then go ahead and build our books for that particular author so let's uh just double check that yep okay so what we're going to do now is go into our urls here and let's now build a a new view for this so this is going to be add so we can just take what we have here and then let's just extend this slightly so authors add slash and now we're going to use a new view for this so let's call this [Music] so it's not going to be a list it's going to be a crate so it's going to be author create view and then we'll call this author or add author maybe add authors or add author okay so now we've got this in place we now need to create this view so let's go back in and build up this view here so um we're going to call this as you saw it's going to be the author create view so this time for this view we're going to be utilizing the create view class pay view so this is going to be create view so let's just bring it in here so we're just going to extend from here the create view so here what we need is we're going to need the model so we're just telling uh telling you what model we're going to use and that's going to be the author model and then the template the template name so here we're going to build a template called author author create dot html okay and also we can define the fields um that we want to update right so uh we know that inside of our models here we've just got name so let's go ahead and just define those so because we're not going to be building here uh we're not going to actually be building our own form we can just ask django to do it for us and just tell them what fields to use so this is one way of doing this of course um so this is a very quick way of building a form so here i'm just going to say name so from the author table i'm just going to use the name and i'm just specifying the fields i want to include in my form that i don't need to build in a forms page here so that quickly builds a form for me so just uh get that going so that's that done um so what we can now do is think about well um maybe we want to return a message so here we're not going to be using ajax or anything like that so we want to send back some sort of confirmation right so let's do that so let's um import the messages from the contrib uh so this is a tool that allows us to kind of generate messages that we can pass over to templates and capture them in templates so we need to ask ourselves when would we want a message sent to the user about an action has taken place well what we need to do is to kind of extend the uh the form valid so here behind the scenes the create view is going to validate the data that's in this form that we're going to send across because we're going to add a new author of course so what we can do is add an action here that does some other checking of the data or perform some other actions once the data has been validated so the data in the form is going to be validated against the the table to make sure it's the right data format and the right amount of data that we're sending across and so on so here what we're going to do is we're going to add a message so we're going to create a message that is actioned when the actual data is inserted into the database so we're going to get self.request and then we're going to we're going to create a message so let's create a message based upon um message success and then here i'm going to have the this is the message that's going to appear the author has been added there we go so that's a simple message we're going to create or send across in the context so that we can display once a user has created a new author in this case so now we need to do is return um so super and then we need to return this data so if you're not too sure what um super is this is essentially given us access to the function here that's embedded or abstracted away here in the create view and it just allows us to kind of update this with this new information okay so there we go okay valid form so if i was reading this through generally i'm going to use the create view here template which has all the features and functionality to collect the data from the form and then collect the data or sorry collect the data from the form and put it into the the database that's essentially what this is going to do for me so all i need to do is specify the model that i want to use to perform this action and then the template i want to then display the data on or the form on and then here i've specified the name and that's the fields i'm going to use for the form for the user to type in the data so here i could create a separate form page here and build up my fields if i wanted to but i'm just going to simplify it here and use fields the fields attribute here so and then i've gone ahead and i've kind of passed in the form uh and i've kind of extended the form validation process so here if the data is valid we're also going to perform this action here of creating a message that we're going to pass over to the the template and just confirm with the user that a an author has been added now this is only going to appear when the form is valid when the data has been approved as acceptable to be entered into the database and then here we're using super here to kind of like i said in general terms overriding the or providing additional information for the va form validation method which is a method that's already pre-created within this abstracted um class here so hopefully that makes sense to you um if you're not too sure i do have the class based view series which you can kind of run through and get a better better understanding of what it is that we're doing here okay so now what we need to do is obviously create our templates that's the next step we've got the url in place and we've also now got the template with the function that we want to include so let's go ahead in our templates add this new file here or to create and so this is going to be um again just extending from the base um that's the first thing that we need to do and then we're going to need a title so let's go ahead and add a title so this is a very same type of process that we've been doing before so we've then got the content so extending from the base we're going to inject the content in the base template and then what we want to now add is a form okay so now we've got our first form the action is nothing so we're going to post this form to this page as essentially what's going to happen here so the action there's nothing in the action here method is going to be post so that's the method of sending this data over to the server post and then inside of here we can add some hidden hidden films uh the form hidden fields maybe you've got some errors so we can place them at the top here now it's worth reading through that if you are using forms and you haven't seen hidden field before um to have a look at how that works and how that then enables you to display any errors so next up we've got the crf token see csrf token so that's going to be needed to send across it because we're posting data to the date to the um to django so we're going to need to send that across to and now we go ahead and display any kind of non-form errors so we may have um set that up to display some non-form errors so that's a a separate error section that you might want to kind of have a little research into so i'll put that there and then we're going to loop through the data okay so how do we collect the data so here we've created a form and you can see here we're referencing the data for this form um as form so remember what we want to get from this form that we've generated in our view so here all we've done here is we've just specified what fields we wanted but the create view behind the scenes is actually going to build the form for us so here we're just specifying for each of the fields that we specified do something here so print them out so let's go ahead and do that and so we're going to print out any field errors and then we've also got then the field labeled tag and then the field so you can see i'm going to output the the label tag which i can then also define in my view and then also the field so let's have a look at this so this will be manual at the moment so let's just double check it's all working okay so what you're going to need to do i just just remember this i just thinking back you're going to need to type in the s there if you're following the code step by step the in the views it should be messages and not message so you'll receive an error there maybe so go back to the homepage let's go into the authors let's just extend this to add which is our new path and now we've got the name okay so we don't have a submit button yet but we can add in a name so let's go ahead and add in our submit button so there's probably two buttons you want to make here um so i'm going to make this p class again inside the form and have this button so this is inside the form so i've created a button here submit the value is not collection it's add author okay so um it doesn't matter what the value is there i could just remove that couldn't i so then you can see i've just uh styled it as per normal the width of 100 and the margin at the bottom is two so that's add author and then i've got the second link so that here on line 24 that's going to be the the link that's going to send me back home so again i'm just using this url um so from a namespace which is not bookstore which is going to be books and then home and then roll button clasp and so on and let's just cancel so let's go back and refresh so now we've got our two buttons in place so let's just add okay so we've tried to add now what's happened behind the scenes is we actually have added the data so if you weren't too sure the data is being added but what's happening now through the process of using this class based view is now expecting by default a redirect which is defined in the model as the get absolute url so what it's trying to do now is actually take you to that item you've just built so what we need to do now is we need to build a new url and a get url method on the model so that we can actually now view what we've just created so this url that we're going to going to build this is going to need to take in a parameter and that's the the primary key of the author so let's go ahead now and set this out so this is going to be a new path and we want to create our link so for example it's going to be author and then we're going to need to set out our data so this is going to be a primary key so it's going to be an integer and then we will name it or refer to the data as pk so that's going to capture the id of the actual author and then generate our link for us so we can individually individually access individual authors on a page so we're going to need a a view for that so let's do that so let's go for um author detail yeah the order detail view dot as view so let's give that a name so that can be the um author view or for details sorry okay so that's the also detail now then what we're going to need to do here um is go now into our model and create this reverse so we've set up the reverse tool up here so we've added the resources for it sorry so now just underneath our name let's set up our reverse so this reverse essentially a way of this is a tool that allows us to build a url and this case with information from the the table we need to get the id from the table for example so we need to kind of generate this url that can be used to actually forward to and be referenced to as a url so there's a little bit of kind of formatting of the data here as well i'm preparing it for that so we can call this method from the model so we're going to call this uh get absolute url because that's what's being expected by our class based view and then we're going to return reverse so we're going to use this reverse tool to basically build a url now a link so we're going to get the books also detail link uh so that's the link that we've just generated here so essentially we're going to get this link the whole kind of path and now we're going to generate this integer here so that's obviously going to come from the database so this is going to be the quarks so additional information pk that's the reference point of course in our url and then self primary key so when we're looking for a specific author we're going to request a specific author here and we're going to use the get url to build this link and then use the um the key there as whatever item is being passed in this at this point or referred to we're going to get that id from the database here and then completely build that url so that we can then go to that url path in our browser so what we're going to need to do now then go back into our url so let's just follow this um we've now we're now going to create the author detail view so within our views here um maybe i'll just make it under here the author detail view so incredibly simple so we're going to just in this case utilize the detail and the detail view so let's uh bring this in the top here so this is going to be a delete a detail this is going to be a class based view that's pre-defined for us pre-created and like it says it's just going to show an individual item so it is expecting one item and it is expecting to be working with one item so all for detail view here so we're going to need to define the model and that's going to equal author yep okay so let's just put semicolon so we've also then just going to define a template as per normal so that's going to equal in this case author detail html okay so let's just go ahead and now build up this page so templates new file auto detail view um so this is where it gets a little bit tricky now because we're really getting into the thick of it now we've set out our basic pages now we want to start to collect data potentially from multiple tables so let's just uh build this up first so we're going to as per normal i'm just going to paste in the the base here extending from the base and then we'll also want to create a title so this time i'm going to create a dynamic title like you can see here because i'm going to create a title based upon the author's name so the title of this page is going to be the author's name so next up i add the block content and now this time at the top of the block content remember i'm passing across a message so what i can do now is check for that message so here's a simple if statement here so if and end if and what i'm going to do now is use a a list so i'm just going to go as class messages for example and i've got this uh list item here which is going to be the message that we're sending across from the from the view so in the view here we've set out a message here the author has been added so that's the message so we can then capture that message where are we detail here we've captured that message so we've created a loop message in messages and then for each message we're going to print that out so there's only one message so i guess we could probably see that an action now assuming that everything is okay so i made a mistake here in the url so i need to type in views of course okay so with that done let's go back into our page and let's go to add so the author's been added so you can see this is going to be working so all authors um we can now see all the authors let's just go to all we need to build a link in a second or sorry add here so let's add a new new item add author we're now presented on the page so we are going now to our redirect here correctly so this is author seven we're now receiving we're now receiving the message so now what we need to do is prepare the rest of the page so the rest of this page so to speak is going to fill up with the author's name and then all the books that have been associated to that author now let's remember here we're just outputting information here we're not using a form here to update or edit or delete any of the data so here we can pull it out utilizing the foreign key and traverse through to the the books table and access the data so let's have a look at how that's done so here back in the author's detailed template let's go ahead now and add a title for this i'm just going to call this author's book author's book details probably not very good name there so this is just going to detail all the books about um that's related to the author so we're going to start off with the author's name so we grab that so we just need to extend there so you can see i'm just creating this kind of deflex inline so i've got two blocks in line and then that's going to be the author's name and then the author's name here so i'm using the title also so i'm just going to capitalize the first letter in that name so author dot name so that's going to display the the name of the author and then we can move on so we've got this little break here the hr line break and now we're going to create a loop here so for book in author books all so here what i'm doing is i'm traversing that that foreign link we have between model um sorry between the author and the book so here we have the foreign key author id to the author here so essentially i'm starting here in this model and i'm using this foreign key referenced by author here and i'm then going and traversing into this book table and collecting information that i need to display so author dot books referring to the the books model and then all as in get all the data that's related to this particular in this case the author so we start off with the author id and then we traverse over into the books table and then look for all the books that associate to that author with that id in the books table so now that gives me a list of books i can now use book so for every item it finds we then loop through each item and refer to each item as book as we look through and then we can extract extract the data from it so the id and of course the title so at this point that's the only fields we have available the title and the id the id being remember an automatic field that's created the primary key when we create a model so let's go back here and complete this so i have a little break there and at the bottom here i'm just going to add two links so first of all softwrap first of all going to have a link here to the edit authors books so this is going to be the link to the page that we're going to build with the inline form where we're going to update edit and add and delete items all in one form so that will take us to the the main form that we're going to build in a minute and you can see i've added some style and styling as per normal and then i'll create the second link which is just going to take me back to all collections so this is the url books authors so if you're not too sure just have a look here also it's now it's probably not a very good name here to call this um list i should maybe call this uh list authors that's probably um a better name in scheme there so let's just go back to my let's just go back to the detail here and the circle is list authors and we probably need to update the other pages so let's just quickly take a look um at this so we're at the home page go to all authors okay cancel okay the reverse field cancel is not found um so we need to sort that out and that's what we just updated remember uh so that needs to that cancel there needs to take me to home so let me just go back into the code let's just get all these templates so we start off with the home that takes us to the authors list now on this author's list here and we should have an euro at the bottom to books home and that's what i was expecting us wi-fi was a bit strange um we had that problem there because that should take us to the home page yeah so okay reverse for off is not found because we've just changed that so that takes us home that button so let's go back to the home page and then let's just update authors to uh list authors so we've got a list of authors on the front page and we've got add author so we don't actually have the link yet for the ad author so let's just uh bring this in now um books the ad author let's go to the url so if you want to add an author remember it's this one here so that is just add author so let's just go back to the home here and update the home with add author online nine so that adds and creates that link so we should now have all authors yep cancel home and then add author and that's where we can add our author so that's where we are the cancel button here works that takes us back home so we're now all linked up so let's just create a new author again new author one that takes us to the author page which is a also at the moment number nine we can now see the name of the author new author one and now we can go ahead and sort out the form so we can edit the books add the books delete the books for this particular author and of course we should then see this list appear here on this page now notice this message appears when we've added but we when we refresh it's then not available so it's a one time only message that we're sending across in the context right so now we come to i guess what is the main component here using inlines so this is a very simple example actually all this build up to what is essentially going to be just a few lines of code but hopefully just taking you through this process of building this whole thing it's maybe giving you a little bit more experience utilizing class-based views and building this form set ready to prepare this data to be edited and saved and updated etc so let's start off with the url right so here what we're going to do is we're going to set out a new path here and this time we're going to utilize the authors and we're going to grab the primary key so we know which author we're editing which books we're reading for and then we go for uh book and then edit so this would be oh we could just uh we could just go for reddit um but it's specifically the books or editing for this author so let's just go for that link there and this time we're going to call this um author books update view as as underscore view okay and then we'll give it a name equals um what can we call this we call this author book edit yeah so let's call this um author books edit so now we have this in place let's go over now to the view and let's build this up so this is going to be a fairly big view that we're going to be building here so at the bottom here then let's go ahead and new class um we're going to call this might as well just copy and paste it author books view edit view there we go and this time we're gonna add two classes so what i want to do in the form is collect one item in the form to edit a one author sorry in this case to edit so i'm gonna bring in the single object mix in right so this single object mixing is a mixes of django mixing that's going to allow me to select what's going to give me the functions um to all the methods to collect one item from the database or select one item so we know which item we're dealing with when we're saving editing and updating our data so that is going to be needed here and i'm also going to bring in the form view okay so this is uh obviously as a name suggests it's a form view so it's going to give us the tools and facilities to help deal and manage and access form capacities so capability sorry so we can update delete edit etc uh so we bring in those two resources so let's go ahead now and put them in our actual class or make them available so we pass them in obviously what's happening now say obviously what's happening now of course is essentially where we're making these classes available within our class here um without going into kind of object orientated terminology so we now have access to these classes tools all these functions and these functions and methods inside of these classes so we can access override some of these and change i guess the settings of these um shall we say um so that we can tailor our application around what the needs of the needs of our application okay i'm sorry i was trying to not make that sound too technical but give it to you or try to make that understandable for those who don't understand object-orientated programming okay so let's go ahead now then and create our model or select our model which is going to be author so that's how we're going to start and then of course let's uh just make a template for this so this is going to be the author books edit dot html there we go so that's going to be our our template for this so let's go into our templates and just just quickly build that there we go so we'll do that in a second so let's go back here so what we're going to do then here let's remember we're building a form now that's going to allow us to view the current books for that author it's going to also allow us to add books for that author and it's also going to allow us to edit books from those that from from that author and also delete items for that author so there's a lot going on here and really we're not doing much because the form view and single mixing object is going to pretty much do the the back end stuff for us we just need to specify a few things here and as well as now providing some kind of overrides so what we want to do is we want to um create an environment where we're selecting uh one item to be edited so we need to kind of define that so potentially we're going to need to define that when we're performing um a request for the data because one thing we're going to do is collect the data from the database and then show it in the form so we're going to show on that page what books have already been associated to that author so we're going to need a get request or we're going to need to capture a get request now remember on the front end when we go to the page and try and collect data we're sending a http get request so here what we're going to do in this function is we're going to define what it is um or the data we want to work with so the individual item that we want to work with in that form that's essentially what we're going to do here so we're just passing in all these items here args and quags so if you don't know what arks and quarks are um then just head over to my um tutorial on on that uh where i explain in a little bit more detail uh what this is so we're going to um uh we're going to set out this kind of variable here in self to make it available in other instances or other functions methods in this class so we pass in self and then self to object and then this will then be accessible outside of this function to be accessed by another method so that's just a way of kind of making that available as a variable outside this method get so what we're going to do then is we're going to say self dot get object so we're going to use this dot get object now this is a piece of code that's essentially going to get one item from the database so get object is part of the single object object mixin now if you're not too sure what's going on here with all of these uh class-based views what you can do is you can go over to a good website called ccbv let's have a look at this and this website here pretty much gives you a rundown of what methods and attributes are available inside so here for example i've selected the form view that's not very clear um this is the form view i don't know why that's hiding that maybe because i'm zoomed in so we've got this form view here that's what we we are using in our code you can see here we've got all the attributes i can maybe override and all the methods i can utilize and override so one thing um that we'll be doing in a minute is utilizing form valid to kind of provide some additional information we did this earlier in actual fact i think so we utilize form valid and that allowed us to add that message if you remember when the data is validated against the form so you can see there all those different methods and then you can drill down to that you can actually then see the code and that's going to help you create overrides et cetera because this is going to describe to you what this part of the class is actually doing what this method is actually doing so this can be an important place to have a look and you can then start overriding things so here it says template name so notice it starts off with equals none so what's happening here then um because we're using this form view here template name now we're kind of overriding that setting and then we're displaying or we're setting what the actual template should be so there's a clear correlation there we bring in this class that has all these attributes and methods in that's what we're doing in our code and then we're just overriding some of these properties or these attributes sorry um in this class that we're kind of is bringing over from the django um from the django back end so these are pre-created classes that we're bringing in here hopefully that's starting to make sense uh so we're going to use a get object and then we're going to set out a query a query set so query set equals and this is where it's going to be author and dot objects so essentially what is happening here then i guess in layman's terms is we are basically getting a the object that we want to work with that's essentially what's happening here so get object and we're going to get a single object and return a single object from the authors table um that's related um to the form to the um page that we're working on to the author that we want to use so what we're doing here is we're just setting that out here in preparation and putting it into the self object so what we're going to return uh super dot get so here essentially we're just running this override a request hugs let's just put in the eggs and quarks okay so essentially what we've done there is we said that when the auth when the user sorry requests a get request to get data we're going to set up the code that's needed so that we can get that individual item from the database and we're going to need that and to kind of move that across over to the form so that when we get the data for the form we know what um we know what or we collect the right author to edit i probably said this i'm probably just going over the same thing and again and again um i know there's a lot there there's a lot of questions there for uh new developers what is super for example um a self object this idea of using self um to access these type of uh essentially variables we're building outside of this function in in other functions inside of this class so a bit of scope understanding scope etc um so we've got the get so that gets information so we just added some extra things there um for that so next up we want to um also post data to the database so we're going to need to do exactly the same thing again um here so again we're going to need to know what data we're going to be working with to post or update based upon the author so let's also now just change that to post and it's going to be pretty much exactly the same code path i'm going to change super dot post um that should be pretty much all i need to do there so that sorts out the post if i name that properly oh excuse me okay there we go so uh now we've got that in place then now we need to also think about the form so we're going to need a form um if we just go back into here uh you can see getform let's have a look at getform here it is so this is the function that we're going to essentially just add some so we're going to utilize to get the actual form set so here it says a def get form self form class equals none so we don't currently have um potentially a form if form class equals none a return form class get form clark so not much is going on there i can explain to you easily so what we're going to do is we're going to use this function and to basically define the uh form set right so for to connect the form set that we're going to build up that we're going to build so get form and so we need to get form and we're going to take in self and form class so just following what the actual equals none okay so and then we return and we're going to return author oh so we're going to make a new form set uh we haven't done that yet so it's going to head so author uh books yeah form set so let's create um so this is what we're going to add in the forms so we're going to create a a form set called author books form set and that's going to be our inline form set so then we go ahead and aspiring south.get form uh quarks oh no it's going to be parentheses um and then instance equals so the instance we want to use so this is the instance we're defining here so so get form so let's just imagine um here this is exactly that get the form i'm now asking the system get form what form do i want to get so here what i'm doing is i'm returning the form that i'm going to define the form so i'm going to define and then also the instance that i'm working with so if we were to read through this now this is what we're doing here so we're going to create a page which is going to show all the books that are associated with an author and allow us within one single form to update edit delete add more items for that author more books so what we're going to need here is we're going to need to be able to identify the individual uh author so here we bring in single object mix in to help us do that we're also going to need to actually run a form and perform actions based upon the requests that we receive from the user whether it be a request for getting data to show on the form or whether we want to post data to add or edit the form um so we're going to bring in form view so here we first specify our model which is author uh our main model author and then the template name so these are just two overrides we're going to create um so we now have an idea of what we're working with and where the actual form is going to be placed on this template right so now we know that the user's going to send a get request to get data about that author the books so therefore what we're going to do is we we're going to need to be able to tell the system here um or the we're going to need to define sorry what author we're working with so what we're going to do is we're going to take from the single object mixing we're going to use the get object and that's going to allow us then to define a query set that's going to help us identify the individual user so get object so we're going to get one item back essentially here that's related to the actual author so that's going to be passing automatically i mean here we're actually defining what author of course and that's just going to be automatically created so with that in mind we now basically run return super and get so what we're doing now really is uh just defining the fact we've provided an override so now we do the same thing in post so again we're just taking everything that we need self request args quarks if needed and then we do the same thing again make it able to find the individual offer that we're going to need so that's going to set up in both cases what author we're working with so now we're going to use get form kind of uh i'm trying to make this uh easy to understand so apologies if i'm contradicting myself a little bit here so get form is going to allow us to define the form that we're going to use which is going to be the author's book form set which we're going to set up shortly in the form and then what we're going to do is we're basically going to take in the instance we're going to take in the instance which is going to be here self object and that's going to be the the individual item that we're working with so at that point the form now now knows which item we're going to be working with so it can extract information or add or delete information corresponding to that particular author okay so it's a little bit abstract so once that's all done what we then need to do is well we just need to save so what we can do is go ahead and access our form uh valid so this is the method that's going to essentially check our form should bring our form in it's going to check our form for any errors so what we can do is a form save so we're going to need to form save to save the form if everything checks out okay and then what we can also do is we can add another message if we wanted to here because we're not using for example ajax here um this this might be a good idea so here i'm just um going to use messages to add again self-request messages success so if there's a success if it successfully saves then we're going to say chains were saved um so this is already brought in top here messages so we'll just add on to that and now what we need to do is return so let's uh let's return and we're going to return a http response redirect uh http response and that's going to be a self dot and then we're just going to set up a separate url for this so successful or just success url url so we're going to create a new method uh new function called success or maybe it should be called get get success url so now what we need to do then is just uh quickly build this out so get url so we're just taking that self and then we return uh reverb reverse um so let's just check see if we've got reverse at the top here so we just need to bring that in from the django urls yep okay and now return reverse um and then we just need to define what it is we want where we want to return so here is going to be books and then this is the uh we're going to send you back to the page where the order details were um so all your ideas the um the author details so let's just double check our urls so the details which is going to be here so author detail so we're going to send them back there and that's obviously going to list out all the items that we've created for that author so that's a success so that's kind of the workflow here so what we're going to need to do now is actually add in um our form so let's go ahead now and build our form and i guess this is really this the simple part here um so in books new file forms.pi so our forms we just need to use a an inline form set a factory so we're going to use inline form set factory and this is going to help us identify uh the the two tables we want to build and work with in order to kind of save this data that we want to save about the author and the book so let's just go ahead and bring in our inline form set factory and then we're just going to need to bring in from from models dot models we're going to need to import both of the tables which is obviously the author table and then also the book table so we need both models there and now we're going to say or author book form set so we're going to create this author book form set variable and inside of here we're going to say inline form set factory and now we just basically need to define our to our true tables so author and book and then we can also then define the fields on the book that we want to use so let's do that fields in this case it's just going to be the title in the book so there we go um that's it that's pretty much all we need to do is a bit of a anti-climax really um being that this tutorial is called inline form set and i've just spent you know 10 seconds of it just creating that hopefully it makes sense what's going on here i bring in the models and now with the inline form set factory i basically just define the models that i'm working with so this is the the child this is the parent model or table also remember the book has the foreign key to the author uh so let's just go and have a look actually might tell us here so you can see when i hover over here i've got the django plugin built and so it tells me that the def inline form set factory parent model and then i can add some other parameters there and then it looks like i've then got the parent model and then model so the secondary model and then i can also have the form form set so somewhere along here will probably be fields i can't see off the top so optional field so we've got fields here um sorry uh just here the fields and so these are just the parameters that you can include so it's handy if you don't have the django plug-in um i think it's just called django there we go an extension and that's going to provide you some additional information when you do hover over some of these django function so okay so hopefully that makes sense it's as simple as that so if you've done this already before for example inlines utilizing uh this in the admin area it's the same type of principles if you remember you define the inlines and then basically you put them together in in a separate area and then basically just define what what models it is you want to use so it does get a little bit more trickier when we start utilizing three models of course but essentially what's happening when we want to run three models so for example we had author foreign key book to author now imagine if we had book details so what we're going to do next in the next tutorial is create a new model called images so the book might have multiple images so we're going to have an image table and that's going to be connected via foreign key to book so we're going to extend this further and gather information about that image as well so that gets a little bit harder but we'll do that in the next tutorial right so now we've got that in place let's go back to our view let's just make sure that we bring this in so from this is going to be from our forms so we want to import our awesome book form set and that's it really with the view i think so everything else is handled so the actual deleting adding data etc that's all going to be handled um by single object and the form view behind the scenes so it does make it particularly it does make it very easy to utilize um once you understand these um these core concepts here um so let's now go over to the template i guess and finish off the template and we're just about there so this is the author books edit template so let's just start off just making sure we can actually get to it so we're here in the orphan nine it doesn't look like it's working maybe we've got some sort of code error here um look yep okay so it looks like we've got an error here in urls line 12. so views all for books so we've made a mistake here i don't think we're going to need that there are we so author and then of course we're going to need it at the end and then we're going to need a comma here apologies so in our views then it looks like it's done in our views now we've got a line 53 problem so we don't have a semicolon okay so there we go so let's go back into our page so edit author books so we just need to hook up this page here so we now um know that we're going to have an off the book edit name so let's go into our let's follow this we go to home and then we go to the author list and we go to the author detail and in the detail we then can click and link so let's add that here in line 24 so that's in my all for detail page so this is going to be if you remember i don't author book edit so it's going to link me to that page right there so i should now be able to refresh this page okay so reverse also book edit with no arguments found not found one pattern tried also okay so let's have a look again okay so you can see what's happened there if i go back we're not actually passing the argument that's needed so we need to actually pass the primary key um which is obviously the primary key of the particular item or so the author that we want to edit the details for so we can collect that like we did previously so here what we can do is type pk and then that's going to equal our author our author dot primary key so that's going to be collected from this page remember we can collect the we've got the name there so we're collecting just the author primary key so that should now be good there we go so edit all for books and here we are now in edit so one thing before we go into the author books edit page and just finish that off let's remember also to bring in the http response i didn't bring that in so down here we're using http response redirect to redirect the user once the item has been added so i just need to add that dependency here in the view before i forget okay so this is ridiculously very easy once you've done this a few times so let's just go ahead and try this so first of all we're going to extend from the base as per normal so we're in the authors books edit page so we're going to set up the page so we can edit items or edit books about for that particular author and that we've selected so first of all we're going to have the title as per normal well that's the author's name at this point and then we've got the content so over um we're extending from the base gonna inject now into the content of the base so first of all we're gonna build a form so let's build a form so notice that we're using encryption type multi-part form data um so if you're not too sure what that means just go ahead and have a look but essentially this is referring to the fact that the data that we're sending in this form there might be multiple types of data for example um so that gives you a rough idea potentially what that what that means um so first of all we'll start off with the hidden fields so again like i said previously have a look at that um if you want to kind of utilize that type of feature in form it's available it's very popular to use for field errors so we have then the csrf token and then any kind of form management form errors and then we have the title update collections so i'm going to say update collection update author collection i'd probably be better at this point so it's up to you what you want to use i'm just going to say update collection so now then so what i'm going to do now is create a for loop so for book underscore form so inform.forms right so to try and understand what is happening here let's just go back into our let's just go back into our form here so let me just softwrap that so we know that at this point we've essentially selected or we're going to be selecting an individual author remember we're going to use the the get and the post here and the get object here to select the individual item that we're working with the author that we're working with so django can predetermine that [Music] we we help django pre-determine that uh so we can set out author.objects.org and get object so it knows what page we're on so django knows what page one it knows what user we're now using so we can pass that over to the form so essentially we could look at it here what we're doing here like i said is we're going to build collect the form data essentially here so we go into our author books form set and forms and what we're doing here is we're just specifying remember our inline factory here the parent model and also the second model that we want to build with now what we're trying to do here is collect all of those books that are related to the author that's what we're doing now so this is the command here essentially so um for any books that we have in this form so the data's now been collected say so it's now collected all the data about the books or all the books that are associated to the author and now what we're doing here is for book underscore author i'll say book underscore form so get the book data if there's any book data in the form in form dot forms so if there are any data there any book data in formstop form we're going to do something so what we're going to do is well we're going to work out first of all if there is a book or not so if book form instance id so we're going to need else here so if there is a book if we do find a book instance inside of our inside of our form data what we're going to do is we're going to display the title so if the form length in general is more than one that does mean um as you might imagine that will mean that we will have data in our form so therefore we print out add another book now of course if the forms length is less than one obviously we don't have any books in our associated to the author yet so what we print out is add a book okay so now what we can do is just uh continue so all we've done here is we've just simply printed out say simply sorry all we've done here is we've just printed out uh any of the books that are currently or just we're going to display any books that are currently associated to our our author now um i appreciate this point that i might not be making much sense here um how that is working and i appreciate that um let's just move on uh first and see if we can come back and i'll try and summarize that so now we're going to create um a form now for well essentially here what we're going to do now is set out some new areas where i can add some data so this is going to give me here three new um three new inputs to allow me to add um any data and what i'll do is i'll when we get this working in a second i'll take remove some of this code so you can kind of see and start to work out what this code is doing um so we're gonna have a line break and then the p tags at the end where we're just going to add some buttons so one button for updating so this is my update button here value update collection button type submit so as long as you've got button type submit i've then got some as per normal some bootstrap uh code there and then we're going to then just have the um button that's going to take us back to the the authors page to show all the books that are currently connected to that author so let's just give this a go we're going to save that let's just some have a look if there's any errors okay we look like looks like we're okay so let's now have a look so over to the homepage then um all authors okay we've got quite a few now and we need to connect these up in a second so that we can actually click on them um let's just do that now that just makes it a lot easier to deal with um so this is going to be in the this is the list isn't it so let's just bring this down so at the bottom of the list here um we have set out a link here to uh cancel that takes us to the home page so let's just put another another button here so that we can oh no sorry we're not making a button sorry we want to make a because here for example in the a tag we want to set out the actual url of each author so if you remember the url is going to come from the model uh in the author here so get the author details so let's just put that in here so ord also author author dot get absolute url okay so that saves that so let's go back now into our page a refresh so all authors we should now put a link from the authors we can to the author's overview here it says save is changes were saved i'm not telling sure maybe why that's come up so now we can go ahead and maybe add edit the author's books so you can see there's no books currently connected to this author so let's um just call this b1 b2 and b3 so now we can update the collection okay so now we've got reverse for auto detail with no arguments not found one pattern tried authors okay so we've got a problem here with the pattern so it looks like books edit so let's go back down here in our i think it's in our views i think we've got a problem in our view so uh books so what we're trying to do here is reverse when it is successful we're reversing our books or to detail now what we're going to need to do here i think i'd put it maybe in the wrong place earlier but what we're going to do here is we're going to need a pass over i'm on a deja vu moment maybe i did it wrong earlier so we're going to need the primary key um yeah i did didn't i i did it on another page didn't i just surely not too long ago um okay so anyway we need the object.pk so let's get the object primary key so we're going to pass over as as well as um the reverse here we're going to also need to pass in the the data that's going to be formulated here in the url remember here in the line 12 we're making the url for the author book edit so we're going to need to pass in that data just to finish off that url so let's go back now and refresh so it looks like it said it's saved um it doesn't look like it's uh added in there so let's just go back to our collection so let's edit the books okay so the books are still there so that functionality seems to be working okay um so let's just go ahead and for example delete the the first and the third one but let's add d to see if this is working so that works okay so we're just not outputting at the moment so the message also page appears changes were saved so that is working okay so it's just the output of the data so let's have a look at this first okay so did you spot the error okay so i actually told you the answer and then i didn't actually do what i told you um to do so if you go back into our urls sorry in no inside of our models remember i said that when we kind of traverse from author to model to collect data about um in this case we're going to collect all the books about that particular author so what we need to do is we need to refer here to the actual foreign key instance in the book but the name the related name so here we're in author here and we're saying oh we want to get some data here in books so to do that we need to actually reference the the actual author which is the foreign key via the related name so if we copy that and go into our detail page here at the moment what i'm trying to do is i'm trying to for where there is books in author traverse to books or this is not working because books isn't referred to remember i'm using the related name book author so i'm going from the author table to the book author foreign key and then getting all the data that's related to this current author and then i'm going to print out the book in this case the id and the title okay so that's why it's not working so let's go back in and let's just start again from fresh so let's add a new author uh new let's call this new author two and so we add a new author let's uh edit the author's books um a1 we'll just close b1 b2 and just i do update collection so now you can see it's outputting the items so this might be absolutely anything like in your context this might be anything this might be orders and products in the order it might be invoice and invoice items here um so we can then edit them again so maybe we want to kind of delete so you can go off and extend this now because we could have drop downs here with data for example from the other from the actual books field uh sorry from the books model so there's a whole lot of things here we could have now kind of start to integrating here but you can see potentially how easy it is using class based views to kind of set this type of form up and how powerful it is and all we've really done is just told it a little bit about what data we want to use so we can now delete individual items and add at the same time so we can perform multiple operations so i'll do b3 and b4 so you can see i've deleted one and added b4 and b3 and deleted b1 book one so there we go so that is the the final um product that we're building hopefully that was useful sorry if it was a little bit long-winded hopefully you enjoyed going through that so i will be following this up with part two actually before i go i did say that we have a look at the the edit here so let's just go ahead now we're going in the all for books edit page to try and understand this a little bit more so let's just remove this table so here i'm um taking the book data as a table so i'm just basically formatting it as a table so that's just a built-in function that i can perform so let's now try and do that so notice now when i take that away that takes away the option of actually adding new items so you can see what that piece of code is there basically based on basically just taking it away so that will provide us the new fields for us to work and you can see here then this if here is going to actually collect the items from the database so have a go try and replicate it and then just slowly change this and become more comfortable understand a little bit more about these naming conventions this is important so when you go into the views remember what i said we could if it makes it easier for you to begin with to understand what the data names are then by means go ahead and use a context object name and that's obviously the name of the data so whatever you want the name of the data to be on the page to access it as then change it otherwise what's going to happen is it's going to use the the model name and potentially an additionality so form we saw list etc so identifies what we're doing so this is why it says book underscore form it's referring to the the name of the model so we can start to kind of change those things around that's where it gets a little bit confusing to begin with how do we actually collect the data so that's important to understand so like i said these hidden fields etc just have a look i've just added them in there just for additional reading for you um they can be useful to use and you can see now that i've used a table um for the form as well as um earlier i think i printed out automatically printed out the field so just some other ways of outputting forms i could have gone into the forms and i could have added um like a form set to this and define my forms and injected classes etc etc and that's available too for this um okay so there we go some additional information really to try and point you towards understanding this a little bit more and there we have it hopefully that was useful like i said if you have got to the end thank you very much for listening all the way to the end i value your feedback if you do have any feedback to try and make these things better maybe i need to let some just some small changes sometimes receive um someone uh suggested before that maybe i should change the the colors or visual studio code maybe the kind of template will make it look nicer to look at any little things like that i'm glad to take any suggestions from you um any input is great but even if it's negative you think it's negative in input or as long as it's constructive feedback um all feedback is good feedback so thank you very much and i'll see you hopefully in the next tutorial where we have a look at three tables with one foreign key so it's going to be a lot quicker in the second one because we've already got the set up here we just need to extend from where we are now
Info
Channel: Very Academy
Views: 9,146
Rating: 5 out of 5
Keywords: python django, django forms, django inline formsets, django 2021, django ecommerce, django project, django, django tutorial, djangotutorial, django tut, django 3, djangoproject, django examples, learn django, django beginners, beginners django, django example, python
Id: OduVfuv44K8
Channel Id: undefined
Length: 107min 49sec (6469 seconds)
Published: Mon Feb 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.