def METHOD_NAME(self, name, value, attrs=None):
    # copy of Django's Select.optgroups, modified to accept filter_value as a
    # third item in the tuple and expose that as a data-filter-value attribute
    # on the final <option>
    groups = []
    has_selected = False
    for index, choice in enumerate(self.choices):
        try:
            (option_value, option_label, filter_value) = choice
        except ValueError:
            # *ChoiceField will still output blank options as a 2-tuple,
            # so need to handle that too
            (option_value, option_label) = choice
            filter_value = None
        if option_value is None:
            option_value = ""
        subgroup = []
        if isinstance(option_label, (list, tuple)):
            # this is an optgroup - we will iterate over the list in the second item of
            # the tuple (which has been assigned to option_label)
            group_name = option_value
            subindex = 0
            choices = option_label
        else:
            # this is a top-level choice; put it in its own group with no name
            group_name = None
            subindex = None
            choices = [(option_value, option_label, filter_value)]
        groups.append((group_name, subgroup, index))
        for choice in choices:
            try:
                (subvalue, sublabel, filter_value) = choice
            except ValueError:
                (subvalue, sublabel) = choice
                filter_value = None
            selected = str(subvalue) in value and (
                not has_selected or self.allow_multiple_selected
            )
            has_selected |= selected
            subgroup.append(
                self.create_option(
                    name,
                    subvalue,
                    sublabel,
                    selected,
                    index,
                    subindex=subindex,
                    filter_value=filter_value,
                )
            )
            if subindex is not None:
                subindex += 1
    return groups