Opened 15 months ago

Last modified 15 months ago

#34319 closed Bug

ValidationError handling during model.validate_constraints — at Version 2

Reported by: Mateusz Kurowski Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Release blocker Keywords: Model, validate_constraints, ValidationError, code, message
Cc: Gagaro Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mateusz Kurowski)

Imagine scenario when i want to explicitly mark a field that model constraint should raise ValidationError for:

class CustomUniqueConstraint(UniqueConstraint):

    def validate(self, *args, **kwargs):
        try:
            value = super().validate(*args, **kwargs)
        except ValidationError as e:
            raise ValidationError(
                {
                    'email': e,
                }
            )
        return value


class AbstractUser(django.contrib.auth.models.AbstractUser):

    class Meta:
        abstract = True
        constraints = [
            CustomUniqueConstraint(
                Lower("email"),
                name="%(app_label)s_%(class)s_email_unique",
            )
        ]

This wont work because:

1425, in validate_constraints
    if e.code == "unique" and len(constraint.fields) == 1:
       ^^^^^^
AttributeError: 'ValidationError' object has no attribute 'code'

Change History (1)

comment:2 by Mateusz Kurowski, 15 months ago

Description: modified (diff)
Summary: Model.validate_constraints check for ValidationError codeValidationError handling during model.validate_constraints
Type: BugCleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top