#34319: Model.validate_constraints check for ValidationError code
-------------------------------------+-------------------------------------
               Reporter:  Mateusz    |          Owner:  nobody
  Kurowski                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.1
  layer (models, ORM)                |       Keywords:  Model,
               Severity:  Normal     |  validate_constraints,
           Triage Stage:             |  ValidationError, code, message
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34319>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701862dbc45b5-28146a6c-1bbd-4551-8fdb-7c095b765aa9-000000%40eu-central-1.amazonses.com.

Reply via email to