Search code examples
djangoformsets

Django formsets: remove invalid forms


I am having trouble getting this accomplished. I have one formset with 5 extra forms. Those 5 forms have a non-required but pre-filled field (requirement). Formset always raises error when submitting which is normal as it sees all the forms as has_changed. I want to be able to ignore the forms that are partially filled but with mandatory fields empty.

Any help is much appreciated.

Thanks.


Solution

  • Instead of calling formset.is_valid() Loop through forms and validate each form, only when it meet specific requirement that you have, ie:

    for form in formset.forms:
        if (form.data['required_field']):
              form.is_valid()