From 65c46d6932c0956d2988d13ec3d9ab3ef9d96d61 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 19 Feb 2025 20:27:30 +0100 Subject: [PATCH] Fixed #35358, Refs #35234 -- Renamed _check() methods to check() for constraints. --- django/contrib/postgres/constraints.py | 2 +- django/db/models/base.py | 2 +- django/db/models/constraints.py | 6 +++--- docs/releases/6.0.txt | 3 ++- docs/topics/checks.txt | 20 ++++++++++++-------- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py index 2eae101d5a..ea06b10d12 100644 --- a/django/contrib/postgres/constraints.py +++ b/django/contrib/postgres/constraints.py @@ -76,7 +76,7 @@ class ExclusionConstraint(BaseConstraint): expressions.append(expression) return ExpressionList(*expressions).resolve_expression(query) - def _check(self, model, connection): + def check(self, model, connection): references = set() for expr, _ in self.expressions: if isinstance(expr, str): diff --git a/django/db/models/base.py b/django/db/models/base.py index 575365e11c..6da65d5afe 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -2458,7 +2458,7 @@ class Model(AltersData, metaclass=ModelBase): continue connection = connections[db] for constraint in cls._meta.constraints: - errors.extend(constraint._check(cls, connection)) + errors.extend(constraint.check(cls, connection)) return errors diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py index 16606e1898..bcd1f9aae8 100644 --- a/django/db/models/constraints.py +++ b/django/db/models/constraints.py @@ -66,7 +66,7 @@ class BaseConstraint: def get_violation_error_message(self): return self.violation_error_message % {"name": self.name} - def _check(self, model, connection): + def check(self, model, connection): return [] def _check_references(self, model, references): @@ -147,7 +147,7 @@ class CheckConstraint(BaseConstraint): violation_error_message=violation_error_message, ) - def _check(self, model, connection): + def check(self, model, connection): errors = [] if not ( connection.features.supports_table_check_constraints @@ -332,7 +332,7 @@ class UniqueConstraint(BaseConstraint): def contains_expressions(self): return bool(self.expressions) - def _check(self, model, connection): + def check(self, model, connection): errors = model._check_local_fields({*self.fields, *self.include}, "constraints") required_db_features = model._meta.required_db_features if self.condition is not None and not ( diff --git a/docs/releases/6.0.txt b/docs/releases/6.0.txt index abc56e6886..aeb38d7874 100644 --- a/docs/releases/6.0.txt +++ b/docs/releases/6.0.txt @@ -177,7 +177,8 @@ Migrations Models ~~~~~~ -* ... +* :doc:`Constraints ` now implement a ``check()`` + method that is already registered with the check framework. Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/checks.txt b/docs/topics/checks.txt index b0e8c2987f..f936ecb00f 100644 --- a/docs/topics/checks.txt +++ b/docs/topics/checks.txt @@ -130,18 +130,18 @@ The code below is equivalent to the code above:: .. _field-checking: -Field, model, manager, template engine, and database checks ------------------------------------------------------------ +Field, constraint, model, manager, template engine, and database checks +----------------------------------------------------------------------- In some cases, you won't need to register your check function -- you can piggyback on an existing registration. -Fields, models, model managers, template engines, and database backends all -implement a ``check()`` method that is already registered with the check -framework. If you want to add extra checks, you can extend the implementation -on the base class, perform any extra checks you need, and append any messages -to those generated by the base class. It's recommended that you delegate each -check to separate methods. +Fields, constraints, models, model managers, template engines, and database +backends all implement a ``check()`` method that is already registered with the +check framework. If you want to add extra checks, you can extend the +implementation on the base class, perform any extra checks you need, and append +any messages to those generated by the base class. It's recommended that you +delegate each check to separate methods. Consider an example where you are implementing a custom field named ``RangedIntegerField``. This field adds ``min`` and ``max`` arguments to the @@ -195,6 +195,10 @@ the only difference is that the check is a classmethod, not an instance method:: # ... your own checks ... return errors +.. versionchanged:: 6.0 + + In older versions, constraints didn't implement a ``check()`` method. + Writing tests -------------