MCPcopy Create free account
hub / github.com/django/django / optgroups

Method optgroups

django/forms/widgets.py:744–782  ·  view source on GitHub ↗

Return a list of optgroups for this widget.

(self, name, value, attrs=None)

Source from the content-addressed store, hash-verified

742 yield from group[1]
743
744 def optgroups(self, name, value, attrs=None):
745 """Return a list of optgroups for this widget."""
746 groups = []
747 has_selected = False
748
749 for index, (option_value, option_label) in enumerate(self.choices):
750 if option_value is None:
751 option_value = ""
752
753 subgroup = []
754 if isinstance(option_label, (list, tuple)):
755 group_name = option_value
756 subindex = 0
757 choices = option_label
758 else:
759 group_name = None
760 subindex = None
761 choices = [(option_value, option_label)]
762 groups.append((group_name, subgroup, index))
763
764 for subvalue, sublabel in choices:
765 selected = (not has_selected or self.allow_multiple_selected) and str(
766 subvalue
767 ) in value
768 has_selected |= selected
769 subgroup.append(
770 self.create_option(
771 name,
772 subvalue,
773 sublabel,
774 selected,
775 index,
776 subindex=subindex,
777 attrs=attrs,
778 )
779 )
780 if subindex is not None:
781 subindex += 1
782 return groups
783
784 def create_option(
785 self, name, value, label, selected, index, subindex=None, attrs=None

Callers 4

optionsMethod · 0.95
get_contextMethod · 0.95
_test_optgroupsMethod · 0.45

Calls 2

create_optionMethod · 0.95
appendMethod · 0.45

Tested by 2

_test_optgroupsMethod · 0.36