29👍
✅
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.
10👍
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']
- Django python manage.py migrate
- How to use bower package manager in Django App?
- Is it possible to generate PDF with StreamingHttpResponse as it's possible to do so with CSV for large dataset?
- Where in Django can I run startup code that requires models?
- How do you extend the Site model in django?
Source:stackexchange.com