Refs #30095 -- Added Field._choices_is_value().

This allows fields classes to override the validation of choices'
values.
This commit is contained in:
Hasan Ramezani 2019-10-31 20:31:14 +01:00 committed by Mariusz Felisiak
parent a20ea33ca6
commit dc60597eb6
1 changed files with 6 additions and 5 deletions

View File

@ -237,13 +237,14 @@ class Field(RegisterLookupMixin):
else: else:
return [] return []
@classmethod
def _choices_is_value(cls, value):
return isinstance(value, (str, Promise)) or not is_iterable(value)
def _check_choices(self): def _check_choices(self):
if not self.choices: if not self.choices:
return [] return []
def is_value(value):
return isinstance(value, (str, Promise)) or not is_iterable(value)
if not is_iterable(self.choices) or isinstance(self.choices, str): if not is_iterable(self.choices) or isinstance(self.choices, str):
return [ return [
checks.Error( checks.Error(
@ -263,7 +264,7 @@ class Field(RegisterLookupMixin):
break break
try: try:
if not all( if not all(
is_value(value) and is_value(human_name) self._choices_is_value(value) and self._choices_is_value(human_name)
for value, human_name in group_choices for value, human_name in group_choices
): ):
break break
@ -275,7 +276,7 @@ class Field(RegisterLookupMixin):
except (TypeError, ValueError): except (TypeError, ValueError):
# No groups, choices in the form [value, display] # No groups, choices in the form [value, display]
value, human_name = group_name, group_choices value, human_name = group_name, group_choices
if not is_value(value) or not is_value(human_name): if not self._choices_is_value(value) or not self._choices_is_value(human_name):
break break
if self.max_length is not None and isinstance(value, str): if self.max_length is not None and isinstance(value, str):
choice_max_length = max(choice_max_length, len(value)) choice_max_length = max(choice_max_length, len(value))