88import re
99import uuid
1010from collections .abc import Mapping
11+ from enum import Enum
1112
1213from django .conf import settings
1314from django .core .exceptions import ObjectDoesNotExist
1718 MinValueValidator , ProhibitNullCharactersValidator , RegexValidator ,
1819 URLValidator , ip_address_validators
1920)
20- from django .db .models import IntegerChoices , TextChoices
2121from django .forms import FilePathField as DjangoFilePathField
2222from django .forms import ImageField as DjangoImageField
2323from django .utils import timezone
@@ -1401,11 +1401,8 @@ def __init__(self, choices, **kwargs):
14011401 def to_internal_value (self , data ):
14021402 if data == '' and self .allow_blank :
14031403 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 ):
14071405 data = data .value
1408-
14091406 try :
14101407 return self .choice_strings_to_values [str (data )]
14111408 except KeyError :
@@ -1414,11 +1411,8 @@ def to_internal_value(self, data):
14141411 def to_representation (self , value ):
14151412 if value in ('' , None ):
14161413 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 ):
14201415 value = value .value
1421-
14221416 return self .choice_strings_to_values .get (str (value ), value )
14231417
14241418 def iter_options (self ):
@@ -1442,8 +1436,7 @@ def _set_choices(self, choices):
14421436 # Allows us to deal with eg. integer choices while supporting either
14431437 # integer or string input, but still get the correct datatype out.
14441438 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
14471440 }
14481441
14491442 choices = property (_get_choices , _set_choices )
@@ -1829,6 +1822,7 @@ class HiddenField(Field):
18291822 constraint on a pair of fields, as we need some way to include the date in
18301823 the validated data.
18311824 """
1825+
18321826 def __init__ (self , ** kwargs ):
18331827 assert 'default' in kwargs , 'default is a required argument.'
18341828 kwargs ['write_only' ] = True
@@ -1858,6 +1852,7 @@ class ExampleSerializer(Serializer):
18581852 def get_extra_info(self, obj):
18591853 return ... # Calculate some data to return.
18601854 """
1855+
18611856 def __init__ (self , method_name = None , ** kwargs ):
18621857 self .method_name = method_name
18631858 kwargs ['source' ] = '*'
0 commit comments