From f95dc69e03501b52b9925535ee0b70674293ef74 Mon Sep 17 00:00:00 2001 From: Saadullah Aleem Date: Tue, 2 May 2023 13:10:10 +0500 Subject: [PATCH] Check for multiple choice enums --- rest_framework/fields.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 00ad60bc2..bccb3a7f5 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -17,7 +17,7 @@ from django.core.validators import ( MinValueValidator, ProhibitNullCharactersValidator, RegexValidator, URLValidator, ip_address_validators ) -from django.db.models import IntegerChoices +from django.db.models import IntegerChoices, TextChoices from django.forms import FilePathField as DjangoFilePathField from django.forms import ImageField as DjangoImageField from django.utils import timezone @@ -1399,7 +1399,8 @@ class ChoiceField(Field): if data == '' and self.allow_blank: return '' - if isinstance(data, IntegerChoices) and str(data) != str(data.value): + if isinstance(data, (IntegerChoices, TextChoices)) and str(data) != \ + str(data.value): data = data.value try: @@ -1411,7 +1412,8 @@ class ChoiceField(Field): if value in ('', None): return value - if isinstance(value, IntegerChoices) and str(value) != str(value.value): + if isinstance(value, (IntegerChoices, TextChoices)) and str(value) != \ + str(value.value): value = value.value return self.choice_strings_to_values.get(str(value), value) @@ -1437,7 +1439,8 @@ class ChoiceField(Field): # Allows us to deal with eg. integer choices while supporting either # integer or string input, but still get the correct datatype out. self.choice_strings_to_values = { - str(key.value) if isinstance(key, IntegerChoices) and str(key) != str( + str(key.value) if isinstance(key, (IntegerChoices, TextChoices)) + and str(key) != str( key.value) else str(key): key for key in self.choices }