Override data validation on one django form element

14,817

Solution 1

The easiest approach would be to define your own method for validating the form, like this:

class MyForm(forms.ModelForm):
    class Meta:
        model = WhateverModel

    def clean(self):
        super(MyForm, self).clean() #if necessary
        if self.cleaned_data.get('film') and 'director' in self._errors:
            del self._errors['director']
        return self.cleaned_data                            

See http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other for a more extensive explanation and http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-clean-method for how it applies to ModelForms.

Solution 2

For some reason the accepted answer didn't work for me (don't know if it's cause things have changed, or I'm using an inline form or what), but overriding full_clean worked:

class MyForm(forms.ModelForm):
    def full_clean(self):
        super(MyForm, self).full_clean()
        if self.cleaned_data.get('film') and 'director' in self._errors:
            del self._errors['director']
Share:
14,817

Related videos on Youtube

Ed.
Author by

Ed.

Updated on May 25, 2020

Comments

  • Ed.
    Ed. almost 4 years

    I have a select list dropdown box on my form that is populated with data from a Model (Directors). The value of this dropdown doesn't need to be saved; it is really only used to dynamically trigger another element of the form (a dropdown titled Films). So when the user chooses a Director, it dynamically populates the second list with the Films attached to that Director.

    The first element of the first list is "All Directors." Instead of filtering the Film List, it lets all Films be shown on the second list because All Directors is chosen.

    If the user chooses a specific Director and then a Film, the form submits correctly. The problem is that if the user chooses All Directors, and then selects a Film, when the form is submitted, it tells me that my choice for Directors is not valid because it is not one of the available choices. In this instance, an available choice (I assume) is one of the existing Director.objects that is in the database. But because I don't care about the Director, I don't need this entry to be valid. I just need Film to be valid.

    I'm using a ModelForm. How can I disable or override the data validation on the Director form field so that it ignores the error that that field generates?

  • prinzdezibel
    prinzdezibel over 10 years
    I second what @Jack says.
  • andilabs
    andilabs about 10 years
    super(MyForm, self).clean() will it allow default model-based validation to be still active?
  • Ytsen de Boer
    Ytsen de Boer about 8 years
    Yes. Before you reach the form's clean method, the field validators will already have been run. You can check if they generated some errors in the "errors" attribute of the form. (Django 1.8)

Related