Skip to content

Commit 549a1f1

Browse files
committed
fix issues on py <3.11
1 parent a50db2b commit 549a1f1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/django_enum/forms.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Enumeration support for django model forms"""
22

3+
import sys
34
from copy import copy
45
from decimal import DecimalException
56
from enum import Enum, Flag
@@ -103,7 +104,14 @@ def format_value(self, value):
103104
# choice tuple to the string conversion of the value
104105
# to determine selected options
105106
if self.enum:
106-
return [str(en.value) for en in self.enum(value)]
107+
if sys.version_info < (3, 11):
108+
return [
109+
str(flg.value)
110+
for flg in self.enum
111+
if flg in self.enum(value) and flg is not self.enum(0)
112+
]
113+
else:
114+
return [str(en.value) for en in self.enum(value)]
107115
if isinstance(value, int):
108116
# automagically work for IntFlags even if we weren't given the enum
109117
return [
@@ -369,6 +377,8 @@ def __init__(
369377

370378
def _coerce(self, value: Any) -> Any:
371379
"""Combine the values into a single flag using |"""
380+
if self.enum and isinstance(value, self.enum):
381+
return value
372382
values = TypedMultipleChoiceField._coerce(self, value) # type: ignore[attr-defined]
373383
if values:
374384
return reduce(or_, values)

0 commit comments

Comments
 (0)