I am new to Django and somewhat confused regarding the validation steps during form processing. I am aware that all form field types (in my case a ModelForm) are required by default. I assumed that Django would raise a VaidationError in case a required form field was left blank without calling the form’s clean method.
This is why I did not check if any data was set in the following clean() method:
def clean(self):
date = self.cleaned_data.get('date')
time_start = self.cleaned_data.get('time_start')
time_end = self.cleaned_data.get('time_end')
user_type = self.cleaned_data.get('user_type')
if Event.objects.filter(user_type=user_type, date=date,
time_start__lt=time_start,
time_end__gt=time_start).exclude(pk=self.instance.pk).count():
raise forms.ValidationError("Overlapping with another event.")
Submitting the form while leaving all fields blank causes a
ValueError: Cannot use None as a query value.
If I remove my clean() method I will get the expected ValidationErrors for not filling out the required fields – which is what I expected with the clean() method still in place.
Any idea what could cause this to happen? I would be surprised if Django does not check for required values before it calls clean.
This is strange because the validation of the fields is performed before the calling of the form’s
cleanmethod. Moreover an error raised from a field is stored inform.my_field.errorswhile the errors returned from the form’s clean method are accumulated inform.non_field_errors.Below is the order of the validations performed in a form: