Check for multiple choice enums

This commit is contained in:
Saadullah Aleem 2023-05-02 13:10:10 +05:00
parent 2d26752e7d
commit f95dc69e03

View File

@ -17,7 +17,7 @@ from django.core.validators import (
MinValueValidator, ProhibitNullCharactersValidator, RegexValidator, MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
URLValidator, ip_address_validators 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 FilePathField as DjangoFilePathField
from django.forms import ImageField as DjangoImageField from django.forms import ImageField as DjangoImageField
from django.utils import timezone from django.utils import timezone
@ -1399,7 +1399,8 @@ class ChoiceField(Field):
if data == '' and self.allow_blank: if data == '' and self.allow_blank:
return '' 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 data = data.value
try: try:
@ -1411,7 +1412,8 @@ class ChoiceField(Field):
if value in ('', None): if value in ('', None):
return value 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 value = value.value
return self.choice_strings_to_values.get(str(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 # Allows us to deal with eg. integer choices while supporting either
# integer or string input, but still get the correct datatype out. # integer or string input, but still get the correct datatype out.
self.choice_strings_to_values = { 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 key.value) else str(key): key for key in self.choices
} }