nice one there

On Sun, Sep 11, 2022 at 3:36 PM David Sanders <shang.xiao.sand...@gmail.com>
wrote:

> Hi folks (and in particular Simon Charette),
>
> I had a bit of a gotcha moment when a custom unique constraint validation
> message disappeared when I added a condition to it. I won't raise a ticket
> for this because it looks intentional from the constraint validation PR,
> but I wanted to seek clarification and whether either some adjustment to
> the behaviour could be made or clarification in the docs.
>
> Here's a boiled down example:
>
> 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.']}
>
> This affects the way custom error messages are defined (eg forms
> error_messages dict vs constraint violation_error_message)
>
> This behaviour is defined in UniqueConstraint.validate() and looks to be
> intentional from this advice from Simon:
> https://github.com/django/django/pull/14625#issuecomment-897275721
>
> I was wondering what the rationale for that was? Would it be possible to
> make the behaviour consistent? And if not - would a PR to clarify how
> ValidationErrors are raised for constraints in the docs be welcomed?
>
> Cheers,
> David
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CADyZw-5w94wvx26OVdSaDHGjKxRGvdnCZYeMOM-p4J9t9JUqMw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-developers/CADyZw-5w94wvx26OVdSaDHGjKxRGvdnCZYeMOM-p4J9t9JUqMw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAPRrvgzm5O110ZRLD9B6YO-kF%2B61tCEmBc-E0z02pCKbeaGJcg%40mail.gmail.com.

Reply via email to