Django-admin clean_fields overrides, saves previous data to send

My question is very simple, I have a class in my application model that inherits from models. Model I am redefining the clean_fields django-admin method to do some custom validation on my form. The problem is that when it raises a ValidationError from my user check, if the user again tries to submit the form with the correct information, it always saves the data from the previous submission.

class SignedOffModelValidation (models.Model):
    class Meta:
        abstract = True

    def clean_fields (self, exclude = None):
        super (SignedOffModelValidation, self) .clean_fields (exclude)

        errors = {}
        if getattr (self, self._meta.immutable_sign_off_field, False):
            relation_fields = [
                f for f in self._meta.fields
                if isinstance (f, (models.ForeignKey, models.ManyToManyField,))
                and not f.name.endswith ('_ ptr')
            ]

            for field in relation_fields:
                try:
                    field_value = getattr (self, field.name)
                    signed_off = getattr (
                        field_value
                        field_value._meta.immutable_sign_off_field
                    )
                except (AttributeError, ObjectDoesNotExist,):
                    continue
                else:
                    if not signed_off:
                        msg = u'In order to signeoff,% s needs to be Signed Off '% \
                            (str (field_value),)
                        errors [field.name] = ([msg])
            if errors:
                raise ValidationError (errors)

Any help would be appreciated!

Regards

+3
source share
1 answer

You should use a method clean(), not clean_fields(). This is pretty clear in the Django documentation .

+1
source

Source: https://habr.com/ru/post/1778930/


All Articles