mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
This commit is contained in:
parent
86493307f9
commit
65c46d6932
@ -76,7 +76,7 @@ class ExclusionConstraint(BaseConstraint):
|
|||||||
expressions.append(expression)
|
expressions.append(expression)
|
||||||
return ExpressionList(*expressions).resolve_expression(query)
|
return ExpressionList(*expressions).resolve_expression(query)
|
||||||
|
|
||||||
def _check(self, model, connection):
|
def check(self, model, connection):
|
||||||
references = set()
|
references = set()
|
||||||
for expr, _ in self.expressions:
|
for expr, _ in self.expressions:
|
||||||
if isinstance(expr, str):
|
if isinstance(expr, str):
|
||||||
|
@ -2458,7 +2458,7 @@ class Model(AltersData, metaclass=ModelBase):
|
|||||||
continue
|
continue
|
||||||
connection = connections[db]
|
connection = connections[db]
|
||||||
for constraint in cls._meta.constraints:
|
for constraint in cls._meta.constraints:
|
||||||
errors.extend(constraint._check(cls, connection))
|
errors.extend(constraint.check(cls, connection))
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ class BaseConstraint:
|
|||||||
def get_violation_error_message(self):
|
def get_violation_error_message(self):
|
||||||
return self.violation_error_message % {"name": self.name}
|
return self.violation_error_message % {"name": self.name}
|
||||||
|
|
||||||
def _check(self, model, connection):
|
def check(self, model, connection):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _check_references(self, model, references):
|
def _check_references(self, model, references):
|
||||||
@ -147,7 +147,7 @@ class CheckConstraint(BaseConstraint):
|
|||||||
violation_error_message=violation_error_message,
|
violation_error_message=violation_error_message,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _check(self, model, connection):
|
def check(self, model, connection):
|
||||||
errors = []
|
errors = []
|
||||||
if not (
|
if not (
|
||||||
connection.features.supports_table_check_constraints
|
connection.features.supports_table_check_constraints
|
||||||
@ -332,7 +332,7 @@ class UniqueConstraint(BaseConstraint):
|
|||||||
def contains_expressions(self):
|
def contains_expressions(self):
|
||||||
return bool(self.expressions)
|
return bool(self.expressions)
|
||||||
|
|
||||||
def _check(self, model, connection):
|
def check(self, model, connection):
|
||||||
errors = model._check_local_fields({*self.fields, *self.include}, "constraints")
|
errors = model._check_local_fields({*self.fields, *self.include}, "constraints")
|
||||||
required_db_features = model._meta.required_db_features
|
required_db_features = model._meta.required_db_features
|
||||||
if self.condition is not None and not (
|
if self.condition is not None and not (
|
||||||
|
@ -177,7 +177,8 @@ Migrations
|
|||||||
Models
|
Models
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
* ...
|
* :doc:`Constraints </ref/models/constraints>` now implement a ``check()``
|
||||||
|
method that is already registered with the check framework.
|
||||||
|
|
||||||
Requests and Responses
|
Requests and Responses
|
||||||
~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -130,18 +130,18 @@ The code below is equivalent to the code above::
|
|||||||
|
|
||||||
.. _field-checking:
|
.. _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
|
In some cases, you won't need to register your check function -- you can
|
||||||
piggyback on an existing registration.
|
piggyback on an existing registration.
|
||||||
|
|
||||||
Fields, models, model managers, template engines, and database backends all
|
Fields, constraints, models, model managers, template engines, and database
|
||||||
implement a ``check()`` method that is already registered with the check
|
backends all implement a ``check()`` method that is already registered with the
|
||||||
framework. If you want to add extra checks, you can extend the implementation
|
check framework. If you want to add extra checks, you can extend the
|
||||||
on the base class, perform any extra checks you need, and append any messages
|
implementation on the base class, perform any extra checks you need, and append
|
||||||
to those generated by the base class. It's recommended that you delegate each
|
any messages to those generated by the base class. It's recommended that you
|
||||||
check to separate methods.
|
delegate each check to separate methods.
|
||||||
|
|
||||||
Consider an example where you are implementing a custom field named
|
Consider an example where you are implementing a custom field named
|
||||||
``RangedIntegerField``. This field adds ``min`` and ``max`` arguments to the
|
``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 ...
|
# ... your own checks ...
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
.. versionchanged:: 6.0
|
||||||
|
|
||||||
|
In older versions, constraints didn't implement a ``check()`` method.
|
||||||
|
|
||||||
Writing tests
|
Writing tests
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user