@@ -1397,7 +1397,8 @@ def __init__(self, choices, **kwargs):
1397
1397
def to_internal_value (self , data ):
1398
1398
if data == '' and self .allow_blank :
1399
1399
return ''
1400
-
1400
+ if isinstance (data , Enum ) and str (data ) != str (data .value ):
1401
+ data = data .value
1401
1402
try :
1402
1403
return self .choice_strings_to_values [str (data )]
1403
1404
except KeyError :
@@ -1406,6 +1407,8 @@ def to_internal_value(self, data):
1406
1407
def to_representation (self , value ):
1407
1408
if value in ('' , None ):
1408
1409
return value
1410
+ if isinstance (value , Enum ) and str (value ) != str (value .value ):
1411
+ value = value .value
1409
1412
return self .choice_strings_to_values .get (str (value ), value )
1410
1413
1411
1414
def iter_options (self ):
@@ -1429,7 +1432,7 @@ def _set_choices(self, choices):
1429
1432
# Allows us to deal with eg. integer choices while supporting either
1430
1433
# integer or string input, but still get the correct datatype out.
1431
1434
self .choice_strings_to_values = {
1432
- str (key ): key for key in self .choices
1435
+ str (key . value ) if isinstance ( key , Enum ) and str ( key ) != str ( key . value ) else str ( key ): key for key in self .choices
1433
1436
}
1434
1437
1435
1438
choices = property (_get_choices , _set_choices )
@@ -1815,6 +1818,7 @@ class HiddenField(Field):
1815
1818
constraint on a pair of fields, as we need some way to include the date in
1816
1819
the validated data.
1817
1820
"""
1821
+
1818
1822
def __init__ (self , ** kwargs ):
1819
1823
assert 'default' in kwargs , 'default is a required argument.'
1820
1824
kwargs ['write_only' ] = True
@@ -1844,6 +1848,7 @@ class ExampleSerializer(Serializer):
1844
1848
def get_extra_info(self, obj):
1845
1849
return ... # Calculate some data to return.
1846
1850
"""
1851
+
1847
1852
def __init__ (self , method_name = None , ** kwargs ):
1848
1853
self .method_name = method_name
1849
1854
kwargs ['source' ] = '*'
0 commit comments