Skip to content

How To Override ValidationError() from Parent Model

W. Bomar edited this page Oct 14, 2021 · 1 revision

Example Code

Intercept Error(s)

from django.core.exceptions import ValidationError

from djangocms_Xxx.models import AbstractXxx

from taccsite_cms.contrib.helpers import (
    get_indices_that_start_with
)

class OurModelThatUsesXxx(AbstractXxx):
    # Validate
    def clean(self):
        # Bypass irrelevant parent validation
        try:
            super().clean()
        except ValidationError as err:
            # Intercept single-field errors
            if hasattr(err, 'error_list'):
                for i in range(len(err.error_list)):
                  # SEE: "Find Error(s)"
                  # ...
                    # Skip error
                    del err.error_list[i]
                    # Replace error
                    # SEE: https://docs.djangoproject.com/en/2.2/ref/forms/validation/#raising-validationerror

            # Intercept multi-field errors
            if hasattr(err, 'error_dict'):
                for field, errors in err.message_dict.items():
                    # SEE: "Find Error(s)"
                    # ...
                        # Skip error
                        del err.error_dict[field]
                        # Replace error
                        # SEE: https://docs.djangoproject.com/en/2.2/ref/forms/validation/#raising-validationerror

            if err.messages:
                raise err

Handle Error(s)

# SEE: "Find Error(s)"
# ...

                    # Catch known static error
                    if 'Known static error string' in error:
                        # ...

                    # Catch known dynamic error
                    indices_to_catch = get_indices_that_start_with(
                        'Known dynamic error string that starts with same text',
                        errors
                    )
                    for i in indices_to_catch:
                        # ...