1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Refs #35234 -- Deprecated CheckConstraint.check in favor of .condition.

Once the deprecation period ends CheckConstraint.check() can become the
documented method that performs system checks for BaseConstraint
subclasses.
This commit is contained in:
Simon Charette
2024-02-26 00:14:26 -05:00
committed by Mariusz Felisiak
parent f82c67aa21
commit daf7d482db
21 changed files with 210 additions and 136 deletions

View File

@@ -77,6 +77,8 @@ details on these changes.
* ``django.urls.register_converter()`` will no longer allow overriding existing
converters.
* The ``check`` keyword argument of ``CheckConstraint`` will be removed.
.. _deprecation-removed-in-5.1:
5.1

View File

@@ -26,7 +26,7 @@ option.
(including ``name``) each time. To work around name collisions, part of the
name may contain ``'%(app_label)s'`` and ``'%(class)s'``, which are
replaced, respectively, by the lowercased app label and class name of the
concrete model. For example ``CheckConstraint(check=Q(age__gte=18),
concrete model. For example ``CheckConstraint(condition=Q(age__gte=18),
name='%(app_label)s_%(class)s_is_adult')``.
.. admonition:: Validation of Constraints
@@ -104,19 +104,19 @@ This method must be implemented by a subclass.
``CheckConstraint``
===================
.. class:: CheckConstraint(*, check, name, violation_error_code=None, violation_error_message=None)
.. class:: CheckConstraint(*, condition, name, violation_error_code=None, violation_error_message=None)
Creates a check constraint in the database.
``check``
---------
``condition``
-------------
.. attribute:: CheckConstraint.check
.. attribute:: CheckConstraint.condition
A :class:`Q` object or boolean :class:`~django.db.models.Expression` that
specifies the check you want the constraint to enforce.
specifies the conditional check you want the constraint to enforce.
For example, ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
For example, ``CheckConstraint(condition=Q(age__gte=18), name='age_gte_18')``
ensures the age field is never less than 18.
.. admonition:: Expression order
@@ -127,7 +127,7 @@ ensures the age field is never less than 18.
reasons. For example, use the following format if order matters::
CheckConstraint(
check=Q(age__gte=18) & Q(expensive_check=condition),
condition=Q(age__gte=18) & Q(expensive_check=condition),
name="age_gte_18_and_others",
)
@@ -138,7 +138,11 @@ ensures the age field is never less than 18.
to behave the same as check constraints validation. For example, if ``age``
is a nullable field::
CheckConstraint(check=Q(age__gte=18) | Q(age__isnull=True), name="age_gte_18")
CheckConstraint(condition=Q(age__gte=18) | Q(age__isnull=True), name="age_gte_18")
.. deprecated:: 5.1
The ``check`` attribute is deprecated in favor of ``condition``.
``UniqueConstraint``
====================

View File

@@ -467,7 +467,7 @@ not be looking at your Django code. For example::
class Meta:
constraints = [
models.CheckConstraint(check=models.Q(age__gte=18), name="age_gte_18"),
models.CheckConstraint(condition=models.Q(age__gte=18), name="age_gte_18"),
]
``verbose_name``

View File

@@ -388,7 +388,7 @@ Models
``OneToOneField`` emulates the behavior of the SQL constraint ``ON DELETE
RESTRICT``.
* :attr:`.CheckConstraint.check` now supports boolean expressions.
* ``CheckConstraint.check`` now supports boolean expressions.
* The :meth:`.RelatedManager.add`, :meth:`~.RelatedManager.create`, and
:meth:`~.RelatedManager.set` methods now accept callables as values in the

View File

@@ -422,6 +422,9 @@ Miscellaneous
* Overriding existing converters with ``django.urls.register_converter()`` is
deprecated.
* The ``check`` keyword argument of ``CheckConstraint`` is deprecated in favor
of ``condition``.
Features removed in 5.1
=======================