From dc60597eb643814e54fbb0f7dadfe68c634e52fa Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Thu, 31 Oct 2019 20:31:14 +0100 Subject: [PATCH] Refs #30095 -- Added Field._choices_is_value(). This allows fields classes to override the validation of choices' values. --- django/db/models/fields/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index fe550169bd9..83f98f74f87 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))