tamakisquare
tamakisquare

Reputation: 17067

Remove invalid form from formset

I have a formset that contains five forms. Each form is simply a file input element. Say, there is some invalid form because a file whose format isn't allowed is being selected. How would I remove this invalid form from the formset and let the formset continues to be processed normally?

Note I tried myformset.forms.pop(i) but that led to "index out of bound" exception in the further processing of the formset.

Upvotes: 2

Views: 694

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

What's the goal here? To only save forms that are valid and simply ignore those that are not?

If that's the case, then simply iterate over the formset and only save the ones that are valid:

for form in formset:
    if form.is_valid():
        form.save()

Upvotes: 5

Related Questions