Skip to content

Commit dfae863

Browse files
committed
handle check field deprecation for django 5.1+
1 parent c6bf667 commit dfae863

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

django_enum/fields.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from functools import reduce
1010
from operator import or_
1111
from typing import Any, Generic, List, Optional, Tuple, Type, TypeVar, Union
12-
12+
from django import VERSION as django_version
1313
from django.core.exceptions import ValidationError
1414
from django.core.validators import DecimalValidator
1515
from django.db.models import (
@@ -84,6 +84,9 @@ class _DatabaseDefault:
8484
PrimitiveT = TypeVar("PrimitiveT", bound=Type[SupportedPrimitive])
8585

8686

87+
condition = "check" if django_version[0:2] < (5, 1) else "condition"
88+
89+
8790
@deconstructible
8891
class EnumValidatorAdapter:
8992
"""
@@ -749,9 +752,10 @@ def contribute_to_class(
749752
constraint |= Q(**{f"{name}__isnull": True})
750753
cls._meta.constraints = [ # pylint: disable=W0212
751754
*cls._meta.constraints, # pylint: disable=W0212
752-
CheckConstraint(
753-
check=constraint, name=self.constraint_name(cls, name, self.enum)
754-
),
755+
CheckConstraint(**{
756+
condition: constraint,
757+
"name": self.constraint_name(cls, name, self.enum)
758+
}),
755759
] # pylint: disable=protected-access
756760
# this dictionary is used to serialize the model, so if constraints
757761
# is not present - they will not be added to migrations
@@ -1176,10 +1180,10 @@ def contribute_to_class(
11761180

11771181
cls._meta.constraints = [ # pylint: disable=W0212
11781182
*cls._meta.constraints, # pylint: disable=W0212
1179-
CheckConstraint(
1180-
check=constraint,
1181-
name=self.constraint_name(cls, name, self.enum),
1182-
),
1183+
CheckConstraint(**{
1184+
condition: constraint,
1185+
"name": self.constraint_name(cls, name, self.enum),
1186+
}),
11831187
]
11841188
# this dictionary is used to serialize the model, so if
11851189
# constraints is not present - they will not be added to

0 commit comments

Comments
 (0)