Skip to content

Commit 1b44e41

Browse files
committed
fixes for django 5
1 parent fa07a0b commit 1b44e41

File tree

10 files changed

+50
-13
lines changed

10 files changed

+50
-13
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ jobs:
1010
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
1111
django-version:
1212
- 'Django~=3.2.0' # LTS April 2024
13-
- 'Django~=4.1.0' # December 2023
1413
- 'Django~=4.2.0' # LTS April 2026
14+
- 'Django~=5.0.0' # April 2025
1515
exclude:
1616
- python-version: '3.7'
17-
django-version: 'Django~=4.1.0'
17+
django-version: 'Django~=5.0.0'
1818
- python-version: '3.7'
1919
django-version: 'Django~=4.2.0'
20+
- python-version: '3.8'
21+
django-version: 'Django~=5.0.0'
22+
- python-version: '3.9'
23+
django-version: 'Django~=5.0.0'
2024
- python-version: '3.11'
2125
django-version: 'Django~=3.2.0'
2226
- python-version: '3.12'
2327
django-version: 'Django~=3.2.0'
24-
- python-version: '3.12'
25-
django-version: 'Django~=4.1.0'
2628

2729
steps:
2830
- uses: actions/checkout@v2

django_enum/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
'EnumFilter'
4848
]
4949

50-
VERSION = (1, 2, 2)
50+
VERSION = (1, 2, 3)
5151

5252
__title__ = 'Django Enum'
5353
__version__ = '.'.join(str(i) for i in VERSION)

django_enum/choices.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
from django.db.models import Choices
1010
from django.db.models import IntegerChoices as DjangoIntegerChoices
1111
from django.db.models import TextChoices as DjangoTextChoices
12-
from django.db.models.enums import ChoicesMeta
12+
13+
try:
14+
from django.db.models.enums import ChoicesType
15+
except ImportError: # pragma: no cover
16+
from django.db.models.enums import ChoicesMeta as ChoicesType
1317

1418

1519
def choices(enum: Optional[Type[Enum]]) -> List[Tuple[Any, str]]:
@@ -87,7 +91,7 @@ def values(enum: Optional[Type[Enum]]) -> List[Any]:
8791
from enum_properties import EnumPropertiesMeta, SymmetricMixin
8892

8993

90-
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesMeta):
94+
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesType):
9195
"""
9296
A composite meta class that combines Django's Choices metaclass with
9397
enum-properties metaclass. This metaclass will add Django's expected
@@ -162,7 +166,7 @@ def __init__(self, *args, **kwargs): # pylint: disable=W0231
162166
DjangoSymmetricMixin = MissingEnumProperties # type: ignore
163167

164168

165-
class DjangoEnumPropertiesMeta(ChoicesMeta): # type: ignore
169+
class DjangoEnumPropertiesMeta(ChoicesType): # type: ignore
166170
"""
167171
Throw error if metaclass is used without enum-properties
168172

django_enum/fields.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class EnumMixin(
8383
enum: Optional[Type[Enum]] = None
8484
strict: bool = True
8585
coerce: bool = True
86+
primitive: Optional[Type[Any]] = None
8687

8788
descriptor_class = ToPythonDeferredAttribute
8889

@@ -91,7 +92,7 @@ def _coerce_to_value_type(self, value: Any) -> Enum:
9192
# note if enum type is int and a floating point is passed we could get
9293
# situations like X.xxx == X - this is acceptable
9394
if self.enum:
94-
return type(values(self.enum)[0])(value)
95+
return self.primitive(value)
9596
# can't ever reach this - just here to make type checker happy
9697
return value # pragma: no cover
9798

@@ -109,6 +110,7 @@ def __init__(
109110
self.coerce = coerce if enum else False
110111
if self.enum is not None:
111112
kwargs.setdefault('choices', choices(enum))
113+
self.primitive = type(values(self.enum)[0])
112114
super().__init__(*args, **kwargs)
113115

114116
def _try_coerce(
@@ -145,6 +147,18 @@ def _try_coerce(
145147
f"{self.enum.__name__} "
146148
f"required by field {self.name}."
147149
) from err
150+
elif (
151+
not self.coerce and self.primitive and
152+
not isinstance(value, self.primitive) and
153+
self.enum and not isinstance(value, self.enum)
154+
):
155+
try:
156+
value = self._coerce_to_value_type(value)
157+
except (TypeError, ValueError) as err:
158+
raise ValueError(
159+
f"'{value}' is not a valid {self.primitive} "
160+
f"required by field {self.name}."
161+
) from err
148162
return value
149163

150164
def deconstruct(self) -> Tuple[str, str, List, dict]:

django_enum/forms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,8 @@ def validate(self, value):
217217
code='invalid_choice',
218218
params={'value': value},
219219
)
220+
221+
def clean(self, value: Any) -> Union[Enum, Any]:
222+
"""Return the value as its full enumeration object"""
223+
value = super().clean(value)
224+
return value

django_enum/tests/djenum/enums.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66

77
class FloatChoices(float, Choices):
8-
pass
8+
9+
def __str__(self):
10+
return str(self.value)
911

1012

1113
class DJIntEnum(IntegerChoices):

django_enum/tests/djenum/views.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class EnumTesterCreateView(URLMixin, CreateView):
4848
fields = '__all__'
4949

5050

51+
def post(self, request, *args, **kwargs):
52+
return super().post(request, *args, **kwargs)
53+
54+
5155
class EnumTesterUpdateView(URLMixin, UpdateView):
5256
model = EnumTester
5357
template_name = 'enumtester_form.html'

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sphinxcontrib-htmlhelp==2.0.1; python_version >= "3.5"
66
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
9-
django-enum==1.2.2
9+
django-enum==1.2.3

doc/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Change Log
33
==========
44

5+
v1.2.3
6+
======
7+
8+
* Added `Support Django 5.0 <https://github.com/bckohan/django-enum/issues/54>`_
9+
510
v1.2.2
611
======
712

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-enum"
3-
version = "1.2.2"
3+
version = "1.2.3"
44
description = "Full and natural support for enumerations as Django model fields."
55
authors = ["Brian Kohan <[email protected]>"]
66
license = "MIT"
@@ -18,6 +18,7 @@ classifiers = [
1818
"Framework :: Django :: 4.0",
1919
"Framework :: Django :: 4.1",
2020
"Framework :: Django :: 4.2",
21+
"Framework :: Django :: 5.0",
2122
"Intended Audience :: Developers",
2223
"License :: OSI Approved :: MIT License",
2324
"Natural Language :: English",
@@ -41,7 +42,7 @@ exclude = ["django_enum/tests"]
4142

4243
[tool.poetry.dependencies]
4344
python = ">=3.7,<4.0"
44-
Django = ">=3.2,<5.0"
45+
Django = ">=3.2,<6.0"
4546
enum-properties = {version = "^1.7.0", optional = true}
4647
django-filter = {version = ">=21,<24", optional = true}
4748
djangorestframework = {version = "^3.9", optional = true}

0 commit comments

Comments
 (0)