Django and empty formset are valid Django and empty formset are valid django django

Django and empty formset are valid


I ran into this question while researching another problem. While digging through the Django source in search of a solution for my problem, I found the answer to this question so I'll document it here:

When a form is allowed to have empty values (this applies for empty forms contained within a formset) and the submitted values haven't been changed from the initial ones, the validation is skipped. Check the full_clean() method in django/forms/forms.py (line 265 in Django 1.2):

# If the form is permitted to be empty, and none of the form data has# changed from the initial data, short circuit any validation.if self.empty_permitted and not self.has_changed():     return

I'm not sure what kind of solution you're looking for (also, this question is already somewhat dated) but maybe this will help someone in the future.


@Jonas, thanks. I used your description to solve my problem. I needed the a form to NOT validate when empty. (Forms added with javascript)

class FacilityForm(forms.ModelForm):    class Meta:        model = Facility    def __init__(self, *arg, **kwarg):        super(FacilityForm, self).__init__(*arg, **kwarg)        self.empty_permitted = False facility_formset = modelformset_factory(Facility, form=FacilityForm)(request.POST)

It will make sure any displayed forms must not be empty when submitted.


The forms created based on the "extra" parameter of "formset_factory" have their "empty_permitted" property set to True. (see: formset.py line 123)

# Allow extra forms to be empty.    if i >= self.initial_form_count():        defaults['empty_permitted'] = True

So it seems the better way to use "initial" parameter of the FormSet and not the "extra" parameter of "formset_factory" for this use case.

Please find the description at using-initial-data-with-a-formset