Return a list of optgroups for this widget.
(self, name, value, attrs=None)
| 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 |