Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django validation calling clean() even when required value missing

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.

like image 692
mzu Avatar asked Sep 16 '12 18:09

mzu


People also ask

What are built-in validations in Django forms?

Built-in Form Field Validations in Django Forms are the default validations that come predefined to all fields. Every field comes in with some built-in validations from Django validators. Each Field class constructor takes some fixed arguments. Some Field classes take additional, field-specific arguments, but required should always be accepted.

How to use required in a field using Django?

Let’s check how to use required in a field using a project. When set to a particular value the option used appends some validations to the field as required by the developer. Let’s try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field.

How do you validate a serializer in Django?

Validation in REST framework Validation in Django REST framework serializers is handled a little differently to how validation works in Django's ModelForm class. With ModelForm the validation is performed partially on the form, and partially on the model instance. With REST framework the validation is performed entirely on the serializer class.

How does the save() method of a Django form work?

The save () of a django form is used to save the data to the database models. But our form does not quiet match with the User model we have used it with. Hence, we need to change how the save () method works. If we modify nothing in the save () method, conceptually (not how it exactly works) it would look something like this.


1 Answers

This is strange because the validation of the fields is performed before the calling of the form's clean method. Moreover an error raised from a field is stored in form.my_field.errors while the errors returned from the form's clean method are accumulated in form.non_field_errors.

Below is the order of the validations performed in a form:

full_clean()
    |
Field clean() [field's built-in clean method]
    |
Form clean_*() [custom validation method for field]
    |
Form clean() [form's clean method]
    |
cleaned_data/errors
like image 163
thikonom Avatar answered Sep 29 '22 14:09

thikonom