[Django] #34319: Model.validate_constraints check for ValidationError code

15 views
Skip to first unread message

Django

unread,
Feb 7, 2023, 4:16:02 PM2/7/23
to django-...@googlegroups.com
#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.

Django

unread,
Feb 7, 2023, 5:20:07 PM2/7/23
to django-...@googlegroups.com
#34319: Model.validate_constraints check for ValidationError code
-------------------------------------+-------------------------------------
Reporter: Mateusz Kurowski | Owner: nobody
Type: Bug | Status: new
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
-------------------------------------+-------------------------------------

Comment (by Mateusz Kurowski):

Maybe all unique constraints should allow raising validation error for
specific field like ?


{{{

class ViolationFieldNameMixin:
"""
Mixin for BaseConstraint subclasses that builds custom
ValidationError message for the `violation_field_name`.
By this way we can bind the error to the field that caused it.
This is useful in ModelForms where we can display the error
message next to the field and also avoid displaying unique
constraint violation error messages more than once for the same
field.
"""

def __init__(self, *args, **kwargs):
self.violation_field_name = kwargs.pop("violation_field_name",
None)
super().__init__(*args, **kwargs)

def validate(self, *args, **kwargs):
try:
value = super().validate(*args, **kwargs)
except ValidationError as e:

# Create a new ValidationError with the violation_field_name
attribute as the key
e = ValidationError({self.violation_field_name: e})
# Set the error code to None
# See https://code.djangoproject.com/ticket/34319#ticket
e.code = "unique"
raise e
return value

def deconstruct(self):
path, args, kwargs = super().deconstruct()
kwargs["violation_field_name"] = self.violation_field_name
return path, args, kwargs

def __eq__(self, other):
return (
super().__eq__(other)
and self.violation_field_name == getattr(other,
"violation_field_name", None)
)


class UniqueConstraint(ViolationFieldNameMixin, models.UniqueConstraint):
...

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34319#comment:1>

Reply all
Reply to author
Forward
0 new messages