#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         |   Keywords:  ModelChoiceField optgroup
 Triage Stage:  Unreviewed     |  Has patch:  1
Easy pickings:  0              |      UI/UX:  0
-------------------------------+---------------------------------------
 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.

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

Reply via email to