how to make a form field not part of the submitted form

Question:

I have an extra field in a django form that triggers javascript to change the fields in other forms:

class MyForm(forms.ModelForm):
    my_extra_form_field = forms.ChoiceField()

    class Meta:
        model = MyModel
        fields = ["field1", "field2"]

    field_order = ["field1", "my_extra_form_field", "field2"]

How can I ensure that my_extra_form_field is not included in the submiteed form?

Asked By: lee King

||

Answers:

Just add this to your javascript in template:

// jQuery
$('#my_extra_form_field').removeAttr('name');

// Javascript
document.getElementById('my_extra_form_field').removeAttribute('name');
Answered By: NixonSparrow

adding to NixionSparrow’s answer:

You may need to add the attribute required as mentioned in the docs here

my_extra_form_field = forms.ChoiceField(required=False)
Answered By: liam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.