Inline Formsets | Django (3.0) Crash Course Tutorials (pt 11)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this video I'm gonna be showing you how we can create objects with the parent object already attached to it so in our example our relationship between a ball and a customer as a parent-child relationship should be created directly from the customer profile and not from a form where we have to select the customer so in our case what I want to do is be able to go to a customer profile and hit place in the order and be able to create the order with the customer information already prefilled so in this case I want to be able to select an order save it and in the backend this will save to this customer so we're first going to do this and then what I'm gonna do is use something called Django in line form sets that allow us to create multiple orders in one form and not have to manually save each item so if I select these four items and save we're gonna notice that for new items were added and if we go to Peter's profile here the are so that's kind of a cool little feature and I had a couple requests for this so I figured it's gonna be worth it to add it to this series I wasn't gonna do it originally but there was enough demand for it and I figured it'll help you guys so let's go ahead and start this okay so the first thing we're gonna do is actually just remove this button here so if we open up our dashboard HTML file it's right above the orders table so we can just take out that link and let's save and refresh and make sure it's gone okay perfect so now what we can do is update our views and our URLs py file so our URL is actually going to look a lot like this update order URL so currently we just have a solid URL and we don't pass any parameters so what we're gonna do here is throw in a primary key so instead of getting the ID of the order we're gonna get the ID of the customer and that's our name to the URL because we're gonna reference that in a second and in our create order a few we're gonna pass in the primary key of the customer so we can only access this view and URL with an instance so we're gonna be a customer is gonna equal to customer dot objects dot get and let me fix that and now we can get the customer by the ID and pass in that primary key here and I can link out to this page now from the customer dashboard so we'll open that up so we can see it and instead of place order we're gonna do create new order from that so we'll just change this to place order and I think I actually added it in earlier so I would already says place order so it was originally delete order I just must have updated that so we'll just change the color really quick to some sort of blue so it'll be the bootstrap blue and now we can link out to it so let's go ahead and pass them that new URL so we're gonna use the template tags here and we're gonna do a URL single quote create underscore order and we'll pass in the customer ID so customer ID and we're able to reference this because when we render out the customer template it's passed in right here so let's go ahead and refresh that and click that link so if we go to place order we get the original form where we still have to select a customer so the first thing we're gonna do is set an instance so this form right here because we've already selected a customer what we're gonna do is pass in an instance of the customer and this is why we query the customer here so now we can do initial is gonna equal two and we'll just pass in a dictionary so we're gonna set the value of the customer parameter and for anybody that's not part of the series I'll just pull up the models really quick and the order has a reference to the customer attribute or the customer model so right here and sorry for those of you that I've already seen this is a little repetitive but just want to make sure everything's covered so the instance of the customer attribute is going to be this customer here so we'll throw that in and now I should see that in my form perfect so it's already Peter so if I go to John Doe and place an order the instance is gonna be John Doe so now I want to actually save this form it'll actually hold those values so I no longer have to manually select two customers so let's go ahead and place a few order so I'll just do barbecue grill and we'll set that status to pending and go back to john doe's profile place another order and this one will set depending again and we'll do dishes and now we're actually seeing items being added to john doe's order or profile directly from his page so we're not having to select the information so now let's actually create the in line form sets where we can place multiple orders from this customers profile so to get started with form sets we actually need to make a quick import into our views WI file so up here under HTTP response I'm gonna do from Jane Doe dot forms import inline form set underscore factory and what form sets are are basically a way for us to create multiple forms within one form so I'm gonna copy this and actually create the first instance so in our create order view here we're gonna do order this is gonna be our variable name so order form set so capital o capital F and capital S and that's going to be equal to that import that we just made and into inline form sets we need to first give it the parrot model and the child model so it's going to have the references from order to customer and we're gonna have multiple orders so we need to tell it which fields that we want to allow for the child objects so I want to allow product and status so we're gonna do fields and that's going to be equal to product and status and now that we've created the instance of the form set we actually need to create the actual form so I'm gonna comment out form and call it form set now so we're gonna do a form set and that's going to be equal to that initial setup and in here all I'm gonna do is pass in the instance of this customer so now that we threw in the parent so we can actually reference that model so we're gonna do instance and that's going to be equal to customer so now what I need to do is change up how we pass it into our template and then the name of the template and that's going to be not customer but form set and in our template here order form I'm going to change that to form set and let's go ahead and try that okay perfect so it's a little ugly now but we're seeing multiple forms now so I'll actually render these out in a different way so I'm gonna do it's actually a list right now so I can do you just that loop template tag so for form in form set and I'm just gonna render out each form individually so I'll just change that to form and just go ahead and close this off so we'll just do n24 and those should be styled so I actually want to create some kind of line break I'll throw in the HR tag there and there we go so we're seeing our initial products and three additional products with that so if we actually go ahead and add these actually won't process this now so I need to go ahead and change up how that request gets handled but we'll actually be able to add multiple products so because of the way I'm doing this it's actually gonna give us a little error here so let me go ahead and just process that and I'll show you the error because a lot of people are having this so we're gonna get form set and we're gonna throw this down here and we're gonna change that and change that to a form set and also pass in the request stop post so we'll do request post and if form set is valid and it's actually this easy so if form set is valid go ahead and save it so right now if I save this it's actually gonna give me an error and I just want you to be funny familiar with this so if you see management form data is missing it's because of the way we're rendering it so if we render this without the loop and just output the forms it would work just fine but because we're doing that we need to add one more one more little thing in that loop so it knows to save each form individually and I'm just gonna copy and paste this and inner loop I'm gonna or just above our loop I'm gonna say form set dot management form and that just sends the data in a different way so if I actually go ahead and Rigo through this process so add in barbecue grill and set that depending and dishes and we'll do pending for that too and submit that that should work fine so those items were just added so what I want to do now is actually show you how we can change the amount of items that get displayed so I first want to take out the initial items I don't want this to show up here I want it only to be new and then how we can set how many tables get displayed so or how many form options get displayed so right here in our initial form set Factory I can do extra and then I can set the value so if I wanted ten different fields I can do that and there we go so we can have as many as we want and in order to hide this right here all I have to do is throw in something into this initial instance right here so I'm gonna do query set and that's going to be equal to order dot objects dot none and that's basically saying if we have objects there don't reference them just let it all be new items so order something we're all here give me a second to figure this out oh and I did object instead of objects I always forget that so now if we do that we're not gonna have that reference so that item is obviously still in there but we can go ahead and just add multiple items and save them so that's how we're able to remove the instances and that covers it for this tutorial with creating objects with parent instances and form sets thanks for watching and in the next video we'll actually get to my Django filter series I was gonna get to that with this video but I thought this one would be more important to throw in before we get to that
Info
Channel: Dennis Ivy
Views: 83,321
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Model Formsets, Inline Formsets, Formset Factory, Django, Python
Id: MRWFg30FmZQ
Channel Id: undefined
Length: 10min 43sec (643 seconds)
Published: Fri Dec 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.