Opened 15 months ago

Last modified 15 months ago

#34319 closed Bug

Model.validate_constraints check for ValidationError code — at Initial Version

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

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'

Simple fix: https://github.com/bukforks/django/commit/9454b2e2abf7eeadbffa50166b217b7b9cc3e2db

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top