# Dynamic ammount of data/forms in a template

**URL:** <https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923>\
**Category:** Forms & APIs\
**Created:** [July 12, 2024, 12:54pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923 "2024-07-12T12:54:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theonlywebhead](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theonlywebhead/32/20720_2.png) [@Theonlywebhead](https://forum.djangoproject.com/u/Theonlywebhead)\
**Post date:** [July 12, 2024, 12:54pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/1 "2024-07-12T12:54:12Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2024, 1:02pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/2 "2024-07-12T13:02:40Z")

</div>

The Django way of creating multiple instances of the same form on a page are [`formsets`.](https://docs.djangoproject.com/en/5.0/topics/forms/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](https://forum.djangoproject.com/t/django-formsets-tutorials/9042)

---

<div class="post-metadata">

**Author:** ![Theonlywebhead](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theonlywebhead/32/20720_2.png) [@Theonlywebhead](https://forum.djangoproject.com/u/Theonlywebhead)\
**Post date:** [July 20, 2024, 7:18pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/3 "2024-07-20T19:18:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [July 21, 2024, 1:03am UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/4 "2024-07-21T01:03:46Z")

</div>

how about models.manytomanyfield and forms.CheckboxSelectMultiple?

it support multiple select in 1 form.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 21, 2024, 3:00am UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/5 "2024-07-21T03:00:25Z")

</div>

> [@Theonlywebhead](#):
>
> However, having the number of items variable this would almost be like a formset inside my form for the order.

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](https://docs.djangoproject.com/en/5.0/ref/forms/api/#prefixes-for-forms))

Also, specifically, the type of feature you’re looking for on this is an [Inline formset](https://docs.djangoproject.com/en/5.0/topics/forms/modelforms/#inline-formsets)

---

<div class="post-metadata">

**Author:** ![Theonlywebhead](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theonlywebhead/32/20720_2.png) [@Theonlywebhead](https://forum.djangoproject.com/u/Theonlywebhead)\
**Post date:** [July 21, 2024, 8:09pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/6 "2024-07-21T20:09:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Theonlywebhead](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theonlywebhead/32/20720_2.png) [@Theonlywebhead](https://forum.djangoproject.com/u/Theonlywebhead)\
**Post date:** [July 23, 2024, 8:32pm UTC](https://forum.djangoproject.com/t/dynamic-ammount-of-data-forms-in-a-template/32923/7 "2024-07-23T20:32:56Z")

</div>

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.
