How to exclude few fields from validation in forms

djangoformsvalidation

I have 10 fields in a Django form like this

class SearchForm(forms.Form):
    student_number                 = forms.CharField(required=False)
    final_date                      = forms.DateField(required=False)
    location_area                   = forms.FloatField(required=False)

In my form.is_valid() I want to exclude few fields from validation like

[location_area, final_date] and rest all perform validation or only want to validate charfields not select fields.

How can I do that?

Best Solution

In the Meta class of form you can exclude fields:

class SearchForm(forms.Form):
    # form fields here
    class Meta:
        exclude = ('location_area', 'final_date',)

If you do not want to exclude fields from form and still don't want to validate them then write a custom field clean method for form which does nothing:

class SearchForm(forms.Form):
    # form fields here

    def clean_location_area(self):
        location_area = self.cleaned_data['location_area']
        return location_area