#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:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Héctor Urbina):

 * needs_docs:   => 0
 * needs_tests:   => 0
 * needs_better_patch:   => 0


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 an optgroup
> HTML element.

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 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27331#comment:1>
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.a545e42a2e747ca160ed3e5eee68d868%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to