#27331: Proposed opt_group argument for ModelChoiceField and
ModelMultipleChoiceField
-------------------------------------+-------------------------------------
     Reporter:  Héctor Urbina        |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Forms                |                  Version:  master
     Severity:  Normal               |               Resolution:  wontfix
     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
-------------------------------------+-------------------------------------

Comment (by angelafoli):

 Replying to [comment:7 Simon Charette]:
 > By the way, this is currently possible with the following code
 >
 > {{{#!python
 > from functools import partial
 > from itertools import groupby
 > from operator import attrgetter
 >
 > class GroupedModelChoiceIterator(ModelChoiceIterator):
 >     def __init__(self, field, groupby):
 >         self.groupby = groupby
 >         super().__init__(field)
 >
 >     def __iter__(self):
 >         if self.field.empty_label is not None:
 >             yield ("", self.field.empty_label)
 >         queryset = self.queryset
 >         # Can't use iterator() when queryset uses prefetch_related()
 >         if not queryset._prefetch_related_lookups:
 >             queryset = queryset.iterator()
 >         for group, objs in groupby(queryset, self.groupby):
 >             yield (group, [self.choice(obj) for obj in objs])
 >
 > class GroupedModelChoiceField(ModelChoiceField):
 >     def __init__(self, *args, choices_groupby, **kwargs):
 >         if isinstance(choices_groupby, str):
 >             choices_groupby = attrgetter(choices_groupby)
 >         elif not callable(choices_groupby):
 >             raise TypeError('choices_groupby must either be a str or a
 callable accepting a single argument')
 >         self.iterator = partial(GroupedModelChoiceIterator,
 groupby=choices_groupby)
 >         super().__init__(*args, **kwargs)
 > }}}
 >
 > While I won't push to get this feature included I think that a request
 for feedback on django-developers has a good chance of being accepted. It
 looks like the main argument against closing this ticket as _wontfix_ was
 the naming of the `ModelChoice(opt_group)` argument which was effectively
 leaking HTML implementation details at the form layer.

 This solution no longer works as of this change:
 
https://github.com/django/django/commit/5ec64f96b2d83ec3c0ef574f52e4767a440017b8

-- 
Ticket URL: <https://code.djangoproject.com/ticket/27331#comment:8>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.c3e9a8baeb0a28f1cec4cfead4f6b50f%40djangoproject.com.

Reply via email to