mirror of https://github.com/django/django.git
Refs #30095 -- Added Field._choices_is_value().
This allows fields classes to override the validation of choices' values.
This commit is contained in:
parent
a20ea33ca6
commit
dc60597eb6
|
@ -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))
|
||||||
|
|
Loading…
Reference in New Issue