﻿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
27331	Proposed opt_group argument for ModelChoiceField and ModelMultipleChoiceField	Héctor Urbina	nobody	"Hello,

I've just implemented this and I thought It could well be incorporated into Django itself; I guess it's a fairly common feature that one may need on any project.

What I propose is to add an optional `opt_group` argument to ModelChoiceField and ModelMultipleChoiceField; which indicates the item's field whose value is used to group the choices. It should be used in conjunction with a queryset which is (primarily) sorted by the same field.

Let me show with an example:
{{{
#!python
class Category(models.Model):
    name = models.CharField(max_length=20)

class Item(models.Model):
    name = models.CharField(max_length=20)
    category = models.ForeignKey(Category)
}}}
And in some form's initialization process
{{{
#!python
field = ModelChoiceField(queryset=Item.objects.order_by('category__name', 'name'), opt_group='category')
}}}
`field.choices` will dynamically collect choices into named groups as a 2-tuple, which the underlying widget should present using `optgroup` HTML elements."	New feature	new	Forms	dev	Normal		ModelChoiceField optgroup		Unreviewed	0	0	0	0	0	0
