#27331: Proposed opt_group argument for ModelChoiceField and
ModelMultipleChoiceField
-------------------------------------+-------------------------------------
Reporter: Héctor Urbina | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: ModelChoiceField | Triage Stage:
optgroup | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Héctor Urbina:
Old description:
> 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 a `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 description:
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27331#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.b2849f58ab964a8369fed0f8d51709ca%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.