diff --git a/media/forms.py b/media/forms.py index 24b5cbd..e0e29e1 100644 --- a/media/forms.py +++ b/media/forms.py @@ -60,6 +60,8 @@ class MediaAdminForm(ModelForm): """ If user fetch ISBN data, then download data before validating the form """ + super().clean() + # TODO implement authors, side_identifier if "_continue" in self.request.POST: isbn = self.cleaned_data.get('isbn') @@ -70,4 +72,25 @@ class MediaAdminForm(ModelForm): # Try with OpenLibrary self.download_data_openlibrary(isbn) - return super().clean() + return self.cleaned_data + + def _clean_fields(self): + for name, field in self.fields.items(): + # value_from_datadict() gets the data from the data dictionaries. + # Each widget type knows how to retrieve its own data, because some + # widgets split data over several HTML fields. + if field.disabled: + value = self.get_initial_for_field(field, name) + else: + value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) + from django.core.exceptions import ValidationError + try: + # We don't want to check a field when we enter an ISBN. + if "_continue" not in self.request.POST or not self.cleaned_data.get('isbn'): + value = field.clean(value) + self.cleaned_data[name] = value + if hasattr(self, 'clean_%s' % name): + value = getattr(self, 'clean_%s' % name)() + self.cleaned_data[name] = value + except ValidationError as e: + self.add_error(name, e)