summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2019-10-31 20:31:14 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-11-05 11:48:44 +0100
commitdc60597eb643814e54fbb0f7dadfe68c634e52fa (patch)
tree80da807deb20534b196d40bca4e0349240989cf3
parenta20ea33ca66bafd38bfce1a73221384b8aa37170 (diff)
Refs #30095 -- Added Field._choices_is_value().
This allows fields classes to override the validation of choices' values.
-rw-r--r--django/db/models/fields/__init__.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index fe550169bd..83f98f74f8 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -237,13 +237,14 @@ class Field(RegisterLookupMixin):
else:
return []
+ @classmethod
+ def _choices_is_value(cls, value):
+ return isinstance(value, (str, Promise)) or not is_iterable(value)
+
def _check_choices(self):
if not self.choices:
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):
return [
checks.Error(
@@ -263,7 +264,7 @@ class Field(RegisterLookupMixin):
break
try:
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
):
break
@@ -275,7 +276,7 @@ class Field(RegisterLookupMixin):
except (TypeError, ValueError):
# No groups, choices in the form [value, display]
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
if self.max_length is not None and isinstance(value, str):
choice_max_length = max(choice_max_length, len(value))