1
0
mirror of https://github.com/django/django.git synced 2025-01-18 14:24:39 +00:00

Refs #35234 -- Removed CheckConstraint.check per deprecation timeline.

This commit is contained in:
Sarah Boyce 2024-12-13 09:30:29 +01:00
parent 4968f0012e
commit 85750bd2f8
4 changed files with 3 additions and 56 deletions

View File

@ -1,4 +1,3 @@
import warnings
from enum import Enum from enum import Enum
from types import NoneType from types import NoneType
@ -12,7 +11,6 @@ from django.db.models.lookups import Exact, IsNull
from django.db.models.query_utils import Q from django.db.models.query_utils import Q
from django.db.models.sql.query import Query from django.db.models.sql.query import Query
from django.db.utils import DEFAULT_DB_ALIAS from django.db.utils import DEFAULT_DB_ALIAS
from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
__all__ = ["BaseConstraint", "CheckConstraint", "Deferrable", "UniqueConstraint"] __all__ = ["BaseConstraint", "CheckConstraint", "Deferrable", "UniqueConstraint"]
@ -130,26 +128,14 @@ class BaseConstraint:
class CheckConstraint(BaseConstraint): class CheckConstraint(BaseConstraint):
# RemovedInDjango60Warning: when the deprecation ends, replace with
# def __init__(
# self, *, condition, name, violation_error_code=None, violation_error_message=None
# )
def __init__( def __init__(
self, self,
*, *,
condition,
name, name,
condition=None,
check=None,
violation_error_code=None, violation_error_code=None,
violation_error_message=None, violation_error_message=None,
): ):
if check is not None:
warnings.warn(
"CheckConstraint.check is deprecated in favor of `.condition`.",
RemovedInDjango60Warning,
stacklevel=2,
)
condition = check
self.condition = condition self.condition = condition
if not getattr(condition, "conditional", False): if not getattr(condition, "conditional", False):
raise TypeError( raise TypeError(
@ -161,24 +147,6 @@ class CheckConstraint(BaseConstraint):
violation_error_message=violation_error_message, violation_error_message=violation_error_message,
) )
def _get_check(self):
warnings.warn(
"CheckConstraint.check is deprecated in favor of `.condition`.",
RemovedInDjango60Warning,
stacklevel=2,
)
return self.condition
def _set_check(self, value):
warnings.warn(
"CheckConstraint.check is deprecated in favor of `.condition`.",
RemovedInDjango60Warning,
stacklevel=2,
)
self.condition = value
check = property(_get_check, _set_check)
def _check(self, model, connection): def _check(self, model, connection):
errors = [] errors = []
if not ( if not (

View File

@ -123,10 +123,6 @@ ensures the age field is never less than 18.
CheckConstraint(condition=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`` ``UniqueConstraint``
==================== ====================

View File

@ -314,3 +314,5 @@ to remove usage of these features.
``Model.asave()`` is removed. ``Model.asave()`` is removed.
* The setter for ``django.contrib.gis.gdal.OGRGeometry.coord_dim`` is removed. * The setter for ``django.contrib.gis.gdal.OGRGeometry.coord_dim`` is removed.
* The ``check`` keyword argument of ``CheckConstraint`` is removed.

View File

@ -7,8 +7,6 @@ from django.db.models.constraints import BaseConstraint, UniqueConstraint
from django.db.models.functions import Abs, Lower, Sqrt, Upper from django.db.models.functions import Abs, Lower, Sqrt, Upper
from django.db.transaction import atomic from django.db.transaction import atomic
from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import ignore_warnings
from django.utils.deprecation import RemovedInDjango60Warning
from .models import ( from .models import (
ChildModel, ChildModel,
@ -399,23 +397,6 @@ class CheckConstraintTests(TestCase):
constraint.validate(model, invalid_product, exclude={"price"}) constraint.validate(model, invalid_product, exclude={"price"})
constraint.validate(model, invalid_product, exclude={"rebate"}) constraint.validate(model, invalid_product, exclude={"rebate"})
def test_check_deprecation(self):
msg = "CheckConstraint.check is deprecated in favor of `.condition`."
condition = models.Q(foo="bar")
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
constraint = models.CheckConstraint(name="constraint", check=condition)
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
self.assertIs(constraint.check, condition)
self.assertEqual(ctx.filename, __file__)
other_condition = models.Q(something="else")
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
constraint.check = other_condition
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
self.assertIs(constraint.check, other_condition)
self.assertEqual(ctx.filename, __file__)
def test_database_default(self): def test_database_default(self):
models.CheckConstraint( models.CheckConstraint(
condition=models.Q(field_with_db_default="field_with_db_default"), condition=models.Q(field_with_db_default="field_with_db_default"),