I have a form which I simplified for brevity’s sake. It takes a csvfile and some other fields, that I simplified away.
class ImportForm(Form):
csvfile = FileField(
validators=[
FileMaxSizeValidator(1024 * 1024 * 1024),
FileExtensionValidator(["csv"]),
]
)
def clean_csvfile(self):
... # Simplified for brevity
# Removed the error condition for simplification
raise ValidationError("CSV field {} missing.".format(rf))
csvfile = list(csvreader)
return csvfile
def clean(self):
cleaned_data = super(ImportForm, self).clean()
csvfile = cleaned_data.get("csvfile")
if(len(csvfile) > 1):
...
... # Shortened for brevity
I am expecting the form to render immediately after the clean_csvfile()
method raises the ValidationError, and show the error message CSV field {} missing.
in the form.
What actually happens instead is that the clean()
method is called, which throws object of type 'NoneType' has no len()
at len(csvfile)
.
I’m confused because I think this was working fine, but I was surprised with the app throwing. But then again, I did some major refactoring and haven’t touched that particular piece of code for some weeks. So maybe I just never tested that code properly with a faulty csvfile.
Is it correct that ValidationErrors in a clean_() still cause clean() to run afterwards? I’m confused and could need some clarification on what’s happening.
Thanks.