Skip to content

Commit 2ce149e

Browse files
authored
Merge pull request #125 from johnthagen/fix-str-representation
Restore str representation conversion logic removed in 2.1.0
2 parents f5eaf45 + 7164aa1 commit 2ce149e

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

enumfields/drf/fields.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ def __init__(self, enum, lenient=False, ints_as_names=False, **kwargs):
1919
super().__init__(**kwargs)
2020

2121
def to_representation(self, instance):
22-
assert isinstance(instance, self.enum), instance
23-
if self.ints_as_names and isinstance(instance.value, int):
24-
# If the enum value is an int, assume the name is more representative
25-
return instance.name.lower()
26-
return instance.value
22+
if instance in ('', None):
23+
return instance
24+
try:
25+
if not isinstance(instance, self.enum):
26+
instance = self.enum(instance) # Try to cast it
27+
if self.ints_as_names and isinstance(instance.value, int):
28+
# If the enum value is an int, assume the name is more representative
29+
return instance.name.lower()
30+
return instance.value
31+
except ValueError:
32+
raise ValueError('Invalid value [{!r}] of enum {}'.format(instance, self.enum.__name__))
2733

2834
def to_internal_value(self, data):
2935
if isinstance(data, self.enum):

tests/enums.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.utils.translation import ugettext_lazy
1+
from django.utils.translation import gettext_lazy
22

33
from enumfields import Enum, IntEnum
44

@@ -12,7 +12,7 @@ class Color(Enum):
1212

1313
class Labels:
1414
RED = 'Reddish'
15-
BLUE = ugettext_lazy('bluë')
15+
BLUE = gettext_lazy('bluë')
1616

1717

1818
class Taste(Enum):

tests/test_serializers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import uuid
22

33
import pytest
4+
from enumfields.drf import EnumField
45
from enumfields.drf.serializers import EnumSupportSerializerMixin
56
from rest_framework import serializers
67

@@ -42,6 +43,22 @@ def test_serialize(int_names):
4243
assert data['int_enum'] == data['int_enum_not_editable'] == IntegerEnum.B.value
4344

4445

46+
@pytest.mark.parametrize('instance, representation', [
47+
('', ''),
48+
(None, None),
49+
('r', 'r'),
50+
('g', 'g'),
51+
('b', 'b'),
52+
])
53+
def test_enumfield_to_representation(instance, representation):
54+
assert EnumField(Color).to_representation(instance) == representation
55+
56+
57+
def test_invalid_enumfield_to_representation():
58+
with pytest.raises(ValueError, match=r"Invalid value.*"):
59+
assert EnumField(Color).to_representation('INVALID_ENUM_STRING')
60+
61+
4562
@pytest.mark.django_db
4663
@pytest.mark.parametrize('lenient_serializer', (False, True))
4764
@pytest.mark.parametrize('lenient_data', (False, True))

0 commit comments

Comments
 (0)