summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/constraints.py
diff options
context:
space:
mode:
authorXavier Fernandez <xavier.fernandez@beta.gouv.fr>2023-02-14 21:06:45 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-23 10:58:20 +0100
commit5b3d3e400ab9334ba429ca360c9818c6dfc3a51b (patch)
tree03bd0bc014819b45f3941917a9faf9e2a8eb5bdc /django/contrib/postgres/constraints.py
parent51c9bb7cd16081133af4f0ab6d06572660309730 (diff)
downloaddjango-5b3d3e400ab9334ba429ca360c9818c6dfc3a51b.tar.gz
Fixed #34338 -- Allowed customizing code of ValidationError in BaseConstraint and subclasses.
Diffstat (limited to 'django/contrib/postgres/constraints.py')
-rw-r--r--django/contrib/postgres/constraints.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py
index ad3a5f61f5..c61072b5a5 100644
--- a/django/contrib/postgres/constraints.py
+++ b/django/contrib/postgres/constraints.py
@@ -32,6 +32,7 @@ class ExclusionConstraint(BaseConstraint):
condition=None,
deferrable=None,
include=None,
+ violation_error_code=None,
violation_error_message=None,
):
if index_type and index_type.lower() not in {"gist", "spgist"}:
@@ -60,7 +61,11 @@ class ExclusionConstraint(BaseConstraint):
self.condition = condition
self.deferrable = deferrable
self.include = tuple(include) if include else ()
- super().__init__(name=name, violation_error_message=violation_error_message)
+ super().__init__(
+ name=name,
+ violation_error_code=violation_error_code,
+ violation_error_message=violation_error_message,
+ )
def _get_expressions(self, schema_editor, query):
expressions = []
@@ -149,12 +154,13 @@ class ExclusionConstraint(BaseConstraint):
and self.condition == other.condition
and self.deferrable == other.deferrable
and self.include == other.include
+ and self.violation_error_code == other.violation_error_code
and self.violation_error_message == other.violation_error_message
)
return super().__eq__(other)
def __repr__(self):
- return "<%s: index_type=%s expressions=%s name=%s%s%s%s%s>" % (
+ return "<%s: index_type=%s expressions=%s name=%s%s%s%s%s%s>" % (
self.__class__.__qualname__,
repr(self.index_type),
repr(self.expressions),
@@ -164,6 +170,11 @@ class ExclusionConstraint(BaseConstraint):
"" if not self.include else " include=%s" % repr(self.include),
(
""
+ if self.violation_error_code is None
+ else " violation_error_code=%r" % self.violation_error_code
+ ),
+ (
+ ""
if self.violation_error_message is None
or self.violation_error_message == self.default_violation_error_message
else " violation_error_message=%r" % self.violation_error_message
@@ -204,9 +215,13 @@ class ExclusionConstraint(BaseConstraint):
queryset = queryset.exclude(pk=model_class_pk)
if not self.condition:
if queryset.exists():
- raise ValidationError(self.get_violation_error_message())
+ raise ValidationError(
+ self.get_violation_error_message(), code=self.violation_error_code
+ )
else:
if (self.condition & Exists(queryset.filter(self.condition))).check(
replacement_map, using=using
):
- raise ValidationError(self.get_violation_error_message())
+ raise ValidationError(
+ self.get_violation_error_message(), code=self.violation_error_code
+ )