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