I'm using Django 2.14 and my set up for this view is a form and a formset.
The code looks like this:
order_form = CreateOrder(instance=order, initial=initial)
item_formset = inlineformset_factory(Order, Item, form=AddItem, validate_min=True, min_num=1, extra=1)
formset = item_formset(instance=order, initial=items_initial)
# form processing
...
I used this setup to add more forms to the formset using javascript.
So everything works fine if I try to add extra forms. The only issue is that I can't remove forms sometimes.
This only happens after I add and remove some fields and refresh the page. The TOTAL_FORMS value is initialized as 1 even if there are two forms (1 initial + 1 extra). It seems like the browser is populating TOTAL_FORMS with the previous value (before the page is refreshed).
<input type="hidden" name="item_set-TOTAL_FORMS" value="1" id="id_item_set-TOTAL_FORMS">
Based on this post from reddit it seems like one solution might be to add the autocomplete='off' tag to stop this behavior.
I don't want to use an external library to solve this problem, so is there any other way to hook on to the formset management form to add these tags?
Would it be any better to use javascript to modify the TOTAL_FORMS value?