[Answer]-KeyError from customized clean() method in BaseModelFormset

0πŸ‘

βœ…

I used the Django shell to call the forms manually. I found that I was executing the clean() method on all of the forms returned from the view. There were 2 filled out with data, and 2 blank. When my clean() method was iterating through them all, it returned a KeyError when it got to the first blank one.

I fixed my issue by using a try-statement and passing on KeyErrors.

πŸ‘€exxy-

1πŸ‘

Isn’t the clean method missing a return statement ? If I remember correctly it should always return the cleaned_data. Also the super call returns the cleaned_data so you should assign it there.

def clean(self):
    cleaned_data = super(BaseTechOnsiteFormset, self).clean()
    # use cleaned_data from here to validate your form
    return cleaned_data

See: the django docs for more information

Leave a comment