Re: [Django] #34319: ValidationError handling during model.validate_constraints (was: Model.validate_constraints check for ValidationError code)

4 views
Skip to first unread message

Django

unread,
Feb 7, 2023, 5:26:45 PM2/7/23
to django-...@googlegroups.com
#34319: ValidationError handling during model.validate_constraints
-------------------------------------+-------------------------------------
Reporter: Mateusz Kurowski | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Model, | Triage Stage:
validate_constraints, | Unreviewed
ValidationError, code, message |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mateusz Kurowski):

* type: Bug => Cleanup/optimization


Old 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

New 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'
}}}

--

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

Reply all
Reply to author
Forward
0 new messages