diff options
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/choices.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/django/utils/choices.py b/django/utils/choices.py index 734b9331a1..54dbdcb3aa 100644 --- a/django/utils/choices.py +++ b/django/utils/choices.py @@ -6,6 +6,7 @@ from django.utils.functional import Promise __all__ = [ "BaseChoiceIterator", "CallableChoiceIterator", + "flatten_choices", "normalize_choices", ] @@ -43,6 +44,15 @@ class CallableChoiceIterator(BaseChoiceIterator): yield from normalize_choices(self.func()) +def flatten_choices(choices): + """Flatten choices by removing nested values.""" + for value_or_group, label_or_nested in choices or (): + if isinstance(label_or_nested, (list, tuple)): + yield from label_or_nested + else: + yield value_or_group, label_or_nested + + def normalize_choices(value, *, depth=0): """Normalize choices values consistently for fields and widgets.""" # Avoid circular import when importing django.forms. |
