﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29059	ChoiceWidget.optgroups() doesn't group choices without a group together	Riccardo Di Virgilio	Abhishek Gautam	"I'm having problems writing template for custom controls (a bootstrap button bar) using optgroup.
the problem is that optgroup is not grouping choices without groups, this makes very hard to create several button bars that grouped togheter:


{{{
from django.forms.widgets import ChoiceWidget

w = ChoiceWidget(choices = [
    (""one"", 1),
    (""two"", 2), [
        ""italian"", (
            (""uno"", 1),
            (""due"", 2)
        )
    ]
])
}}}

now see how choices are grouped from optgroup:


{{{
import json

print(json.dumps(w.optgroups(""name"", ""one""), indent = 4))
}}}


{{{
[
    [
        null,
        [
            {
                ""name"": ""name"",
                ""value"": ""one"",
                ""label"": 1,
                ""selected"": true,
                ""index"": ""0"",
                ""attrs"": {
                    ""checked"": true
                },
                ""type"": null,
                ""template_name"": null
            }
        ],
        0
    ],
    [
        null,
        [
            {
                ""name"": ""name"",
                ""value"": ""two"",
                ""label"": 2,
                ""selected"": false,
                ""index"": ""1"",
                ""attrs"": {},
                ""type"": null,
                ""template_name"": null
            }
        ],
        1
    ],
    [
        ""italian"",
        [
            {
                ""name"": ""name"",
                ""value"": ""uno"",
                ""label"": 1,
                ""selected"": false,
                ""index"": ""2_0"",
                ""attrs"": {},
                ""type"": null,
                ""template_name"": null
            },
            {
                ""name"": ""name"",
                ""value"": ""due"",
                ""label"": 2,
                ""selected"": false,
                ""index"": ""2_1"",
                ""attrs"": {},
                ""type"": null,
                ""template_name"": null
            }
        ],
        2
    ]
]
}}}

this means that if choices are flats (no groups) you are getting a number of groups that is the same of the number of choices, and in order to fix this you need to use the grouping filters on the template in order to regroup choices.

I think this is a bug, and flat choices should be grouped together as None by optgroup method

thanks"	Bug	closed	Forms	2.0	Normal	wontfix			Accepted	1	0	0	1	0	0
