Home > OS >  how to make a form field not part of the submitted form
how to make a form field not part of the submitted form

Time:10-19

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?

CodePudding user response:

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)

CodePudding user response:

Just add this to your javascript in template:

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

// Javascript
document.getElementById('my_extra_form_field').removeAttribute('name');
  • Related