#34007: Single-field conditional UniqueContraint validation errors are 
clasified as
non-field-errors
-----------------------------------------+------------------------
               Reporter:  David Sanders  |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  4.1
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 Mailing list discussion with Simon Charette: https://groups.google.com/g
 /django-developers/c/7QGy6i9oe4g/m/iCJE7be0AgAJ

 Validating models with UniqueConstraint usually groups errors by field
 name:

 {{{
 class Test(Model):
     test = CharField(max_length=255)

     class Meta:
         constraints = [
             UniqueConstraint(fields=["test"], name="unique"),
         ]

 Test.objects.create(test="abc")
 test = Test(test="abc")
 test.validate_constraints()
 django.core.exceptions.ValidationError: {'test': ['Test with this Test
 already exists.']}
 }}}

 However if a condition is added to the UniqueConstraint the validation
 error is categorised as a non-field error:

 {{{
 class Test(Model):
     test = CharField(max_length=255)

     class Meta:
         constraints = [
             UniqueConstraint(fields=["test"], name="unique",
 condition=~Q(test="")),
         ]

 Test.objects.create(test="abc")
 test = Test(test="abc")
 test.validate_constraints()
 django.core.exceptions.ValidationError: {'__all__': ['Constraint “unique”
 is violated.']}
 }}}

 My proposal is to make the validation error a field-error for any unique
 constraint that only has a single field defined.

 This affects the way custom error messages are defined (eg forms
 error_messages dict vs constraint violation_error_message).

 It's been agreed that the error message wording should remain the generic
 "Constraint is violated" format as determining the correct wording
 involving conditions is non-trivial.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34007>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701833acaf64c-5bbe74ec-4a0d-44f6-862e-8a888fd7ecdf-000000%40eu-central-1.amazonses.com.

Reply via email to