diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2023-10-16 19:07:49 +0100 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2023-10-23 13:44:23 -0300 |
| commit | 74afcee234f8be989623ccc7c28b9fb97fb548f0 (patch) | |
| tree | ff429a675532fd47b1c957ad48ba8b012515f8ef /django/utils | |
| parent | 07fa79ef2bb3e8cace7bd87b292c6c85230eed05 (diff) | |
Refs #34899 -- Extracted Field.flatchoices to flatten_choices helper function.
Co-authored-by: Natalia Bidart <124304+nessita@users.noreply.github.com>
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. |
