Skip to content

Commit bb2ce49

Browse files
committed
remove errant attributes from non-strict field option renderings
1 parent 63cba3c commit bb2ce49

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

django_enum/fields.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ def EnumField( # pylint: disable=C0103
333333
334334
.. code-block::
335335
336-
class MyModel(models.Model):
336+
class MyModel(models.Model):
337337
338-
class EnumType(IntegerChoices):
338+
class EnumType(IntegerChoices):
339339
340-
VAL1 = 1, _('Value 1')
341-
VAL2 = 2, _('Value 2')
342-
VAL3 = 3, _('Value 3')
340+
VAL1 = 1, _('Value 1')
341+
VAL2 = 2, _('Value 2')
342+
VAL3 = 3, _('Value 3')
343343
344-
field_name = EnumField(EnumType)
344+
field_name = EnumField(EnumType)
345345
346346
:param enum: The class of the enumeration.
347347
:param field_args: Any standard unnamed field arguments for the underlying

django_enum/forms.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class NonStrictSelect(Select):
2323
A Select widget for non-strict EnumChoiceFields that includes any existing
2424
non-conforming value as a choice option.
2525
"""
26+
2627
choices: Iterable[Tuple[Any, str]]
2728

2829
def render(self, *args, **kwargs):
2930
"""
3031
Before rendering if we're a non-strict field and our value is not
3132
one of our choices, we add it as an option.
3233
"""
34+
3335
value: Any = getattr(kwargs.get('value'), 'value', kwargs.get('value'))
3436
if (
35-
value not in self.attrs.get('empty_values', [])
37+
value not in EnumChoiceField.empty_values
3638
and value not in (
3739
choice[0] for choice in self.choices
3840
)
@@ -120,6 +122,7 @@ def _coerce(self, value: Any) -> Union[Choices, Any]:
120122
non-strict field and the value is of a matching primitive type
121123
:raises ValidationError if a valid return value cannot be determined.
122124
"""
125+
123126
if value in self.empty_values:
124127
return self.empty_value
125128
if self.enum is not None and not isinstance(value, self.enum):
@@ -141,12 +144,6 @@ def _coerce(self, value: Any) -> Union[Choices, Any]:
141144
) from err
142145
return value
143146

144-
def widget_attrs(self, widget: Widget) -> Dict[Any, Any]:
145-
attrs = super().widget_attrs(widget)
146-
attrs.setdefault('empty_value', self.empty_value)
147-
attrs.setdefault('empty_values', self.empty_values)
148-
return attrs
149-
150147
def prepare_value(self, value: Any) -> Any:
151148
"""Must return the raw enumeration value type"""
152149
value = self._coerce(value)

django_enum/tests/tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from django.test import Client, TestCase
1313
from django.urls import reverse
1414
from django_enum import TextChoices
15-
from django_enum.forms import EnumChoiceField
1615
from django_enum.tests.djenum.enums import (
1716
BigIntEnum,
1817
BigPosIntEnum,
@@ -1059,10 +1058,10 @@ def verify_form(self, obj, soup, non_strict_options=False):
10591058
self.assertEqual(getattr(obj, field.name), value)
10601059
del expected[value]
10611060
except KeyError: # pragma: no cover
1062-
import pdb
1063-
pdb.set_trace()
10641061
self.fail(
1065-
f'{field.name} did not expect option {option["value"]}: {option.text}.')
1062+
f'{field.name} did not expect option '
1063+
f'{option["value"]}: {option.text}.'
1064+
)
10661065

10671066
self.assertEqual(len(expected), 0)
10681067

0 commit comments

Comments
 (0)