[Fixed]-How to debug a Django MultiValueDictKeyError on Formset POST

1👍

I stopped this error by adding attachment_ptr to the field list of my form. So DocumentInlineForm is now:

class DocumentInlineForm(forms.ModelForm):  # pylint: disable=R0924
    attachment_file = forms.FileField(widget=NoDirectoryClearableFileInput)
    notes = forms.CharField(
        required=False,
        widget=forms.Textarea(attrs={'rows': 2,}), 
    )
    helper = DocumentInlineFormHelper()

    class Meta: # pylint: disable=W0232,R0903
        fields = (
            'attachment_ptr',
            'attachment_file', 
            'creation_date',
            'document_type',
            'last_modified_date',
            'name',
            'notes',
        )
        model = Document

Maybe it is something I didn’t know before, but does Django require you to provide a pointer to the superclass in all forms that use a subclassed model? This surprises me.

I’d like to find out why this pointer field is required, so I’ve opened a question to address that here: Why does my django formset need a pointer field reference?.

👤Erik

50👍

This isn’t the case with OP, but you will encounter a MultiValueDictKeyError, if some of the hidden fields are missing in the template. It may happen when instead of quick and dirty {{form}}, fields are listed in the template one by one: {{form.field1}}, {{form.field2}}, while leaving out required hidden fields.

To include them back do something along the lines (for each form/form in formset):

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

or

{% for form in formset %}    
    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}    
{% endfor %}

4👍

I use also

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

like this one

<div role="tabpanel" class="tab-pane active" id="email">
                {% csrf_token %}
                {{ eformset.management_form}}
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div id="addemail" class="btn btn-success">
                                <span class="glyphicon glyphicon-plus" > 
                                </span>
                            </div>
                            <p><br></p>
                            {% for f in eformset %}
                                {% for hidden in f.hidden_fields %}
                                    {{ hidden }}
                                {% endfor %}
                                <div class="item_email_set">
                                    <table class="table table-condensed table-bordered">
                                    <tr>
                                        {% for field in f.visible_fields %} <!---->
                                            <td>
                                                {{ field.label }}
                                            </td>
                                        {% endfor %}

                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        {% for field in f.visible_fields %}
                                            <td>
                                                {{field.errors.as_ul}}
                                                {{field}}
                                            </td>
                                        {% endfor %}    
                                        <td class="btncolumn">      
                                            <p style="">
                                                <a class="delete_email_set" href="#">
                                                    <div  class="btn btn-danger">
                                                        <span class="glyphicon glyphicon-remove" > 
                                                        </span>
                                                    </div>
                                                </a>
                                            </p>
                                        </td>
                                    </tr>   

                                </table>
                            </div>  
                        {% endfor %}
                    </div>
                </div>  
            </div>

and I resolve the MultiValueDictKeyError

0👍

This is not the answer to the OP but I got the same error. I mistakenly removed {{ subform.id }} from my template as I couldn’t visually see it and I was tidying up old code.
In your HTML you will get something like:

<input name="note_set-0-id" value="34632" id="id_note_set-0-id" type="hidden">
👤hum3

Leave a comment