8
8
import re
9
9
import uuid
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
17
18
MinValueValidator , ProhibitNullCharactersValidator , RegexValidator ,
18
19
URLValidator , ip_address_validators
19
20
)
20
- from django .db .models import IntegerChoices , TextChoices
21
21
from django .forms import FilePathField as DjangoFilePathField
22
22
from django .forms import ImageField as DjangoImageField
23
23
from django .utils import timezone
@@ -1401,11 +1401,8 @@ def __init__(self, choices, **kwargs):
1401
1401
def to_internal_value (self , data ):
1402
1402
if data == '' and self .allow_blank :
1403
1403
return ''
1404
-
1405
- if isinstance (data , (IntegerChoices , TextChoices )) and str (data ) != \
1406
- str (data .value ):
1404
+ if isinstance (data , Enum ) and str (data ) != str (data .value ):
1407
1405
data = data .value
1408
-
1409
1406
try :
1410
1407
return self .choice_strings_to_values [str (data )]
1411
1408
except KeyError :
@@ -1414,11 +1411,8 @@ def to_internal_value(self, data):
1414
1411
def to_representation (self , value ):
1415
1412
if value in ('' , None ):
1416
1413
return value
1417
-
1418
- if isinstance (value , (IntegerChoices , TextChoices )) and str (value ) != \
1419
- str (value .value ):
1414
+ if isinstance (value , Enum ) and str (value ) != str (value .value ):
1420
1415
value = value .value
1421
-
1422
1416
return self .choice_strings_to_values .get (str (value ), value )
1423
1417
1424
1418
def iter_options (self ):
@@ -1442,8 +1436,7 @@ def _set_choices(self, choices):
1442
1436
# Allows us to deal with eg. integer choices while supporting either
1443
1437
# integer or string input, but still get the correct datatype out.
1444
1438
self .choice_strings_to_values = {
1445
- str (key .value ) if isinstance (key , (IntegerChoices , TextChoices ))
1446
- and str (key ) != str (key .value ) else str (key ): key for key in self .choices
1439
+ str (key .value ) if isinstance (key , Enum ) and str (key ) != str (key .value ) else str (key ): key for key in self .choices
1447
1440
}
1448
1441
1449
1442
choices = property (_get_choices , _set_choices )
@@ -1829,6 +1822,7 @@ class HiddenField(Field):
1829
1822
constraint on a pair of fields, as we need some way to include the date in
1830
1823
the validated data.
1831
1824
"""
1825
+
1832
1826
def __init__ (self , ** kwargs ):
1833
1827
assert 'default' in kwargs , 'default is a required argument.'
1834
1828
kwargs ['write_only' ] = True
@@ -1858,6 +1852,7 @@ class ExampleSerializer(Serializer):
1858
1852
def get_extra_info(self, obj):
1859
1853
return ... # Calculate some data to return.
1860
1854
"""
1855
+
1861
1856
def __init__ (self , method_name = None , ** kwargs ):
1862
1857
self .method_name = method_name
1863
1858
kwargs ['source' ] = '*'
0 commit comments