Dynamic ammount of data/forms in a template

I’m trying to build an order system.

One large order consists of several items with a quantity and a designated user.
As such myself would order something for several other collegues.

This mandates a dynamic amount of items to add: after I added one, another line should appear making it possible to add another item.

I thought about making a form for the item and using javascript to generate more of these and then capturing that data to build the objects in the view but I’m stuck since my view isn’t expecting more then one form.

SHould I just build an html-form and capture that data not relying on Django?
What’s the django-wat to deal with this ?

The Django way of creating multiple instances of the same form on a page are formsets.. They’re designed to facilitate exactly what you describe.

Note: You will need to write the JavaScript on the front-end to create the new instances of the form, but Django can handle the submitted data if those new forms are created correctly.

You may also find this thread and the links on it to reference some useful information: Django formsets tutorials

Hey Ken,

Thanks for the input. This does exactly what I need and I’m able to add parts dynamically.
However, I need the dynamically added parts to be part of one general order.
Think of a shopping cart. The dynamical parts are items with details.

I had the idea of building an order model with a manytomany through relation allowing me to specify the items and additional data.

However, having the number of items variable this would almost be like a formset inside my form for the order.

How would you go about this. I thought about an order form and a separate formset creating the objects and linking them to the data from the order form?

how about models.manytomanyfield and forms.CheckboxSelectMultiple?

it support multiple select in 1 form.

Yes! (If you’re talking about both the form and formset being within the same HTML <form ...> element, and not talking about having the Django formset being a field within a Django form. See Prefixes for forms)

Also, specifically, the type of feature you’re looking for on this is an Inline formset

Great guys,

I feel like I’m closing in on the issue.

However in my particular case I’m using, I think, a true many to many field.

I’m creating an order in my name but for different people. I’m paying but the large coffee is for Ken and the donut for my wife.
As such the food object needs to be “augmented” with a person.

I created an intermediate model “food_person” and used a form called order, food as manytomany through food_person.

The complexity arises when adding a variable amount of items for diverse people.

Am I wrong to assume the online formset factory is not my solution?

I’m getting somewhat lost in the amount of possibilities.

Ok I think I’m there.

I just get the “order” form and the “items” formset both in the same form tags and then using the prefix filter my data, get the validated data and do my thing with it.

This is not for the faint hearted.