Question

I have use-cases, when a formset does not get rendered in the page. In this case request.POST does not contain management data.

How can I manually add missing management data? request.POST QueryDict is read only.

As the answer suggests, I used:

request_post_copy = request.POST.copy()
if not request_post_copy.has_key('form-TOTAL_FORMS'):
    request_post_copy.update({
        'form-TOTAL_FORMS': 0,
        'form-INITIAL_FORMS': 0,
        'form-MAX_NUM_FORMS': 1000,
    })
Was it helpful?

Solution

If the error is that management form data is missing, then management form data IS missing.

Take a look at this: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-a-formset-in-views-and-templates

In the example they use this to display additional fields:

{{ formset.management_form }}

And if those fields are missing then you get the error you are seeing.

Edit:

If you want to add management fields manually to data, then you need to add 3 key/value pairs for fields like :

<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" />
<input type="hidden" name="form-INITIAL_FORMS" value="3" id="id_form-INITIAL_FORMS" />
<input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" />

Do something like this:

my_post_dict = request.POST.copy()
my_post_dict['form-TOTAL_FORMS'] = value
my_post_dict['form-INITIAL_FORMS'] = value
my_post_dict['form-MAX_NUM_FORMS'] = value
myformset = MyFormSet(my_post_dict)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top