Login

ModelChoiceField with optiongroups

Author:
anentropic
Posted:
March 23, 2010
Language:
Python
Version:
1.1
Tags:
modelchoicefield optiongroup
Score:
1 (after 1 ratings)

This is a ModelChoiceField where the choices are rendered in optiongroups (this is already posible with a normal Choicefield)

For this to work properly the queryset you supply should already be ordered the way you want (i.e. by the group_by_field first, then any sub-ordering)

See related blog article

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.forms.models import ModelChoiceIterator, ModelChoiceField

class GroupedModelChoiceField(ModelChoiceField):
    def __init__(self, group_by_field, group_label=None, *args, **kwargs):
        """
        group_by_field is the name of a field on the model
        group_label is a function to return a label for each choice group
        """
        super(GroupedModelChoiceField, self).__init__(*args, **kwargs)
        self.group_by_field = group_by_field
        if group_label is None:
            self.group_label = lambda group: group
        else:
            self.group_label = group_label
    
    def _get_choices(self):
        """
        Exactly as per ModelChoiceField except returns new iterator class
        """
        if hasattr(self, '_choices'):
            return self._choices
        return GroupedModelChoiceIterator(self)
    choices = property(_get_choices, ModelChoiceField._set_choices)

class GroupedModelChoiceIterator(ModelChoiceIterator):
    def __iter__(self):
        if self.field.empty_label is not None:
            yield (u"", self.field.empty_label)
        if self.field.cache_choices:
            if self.field.choice_cache is None:
                self.field.choice_cache = [
                    (self.field.group_label(group), [self.choice(ch) for ch in choices])
                        for group,choices in groupby(self.queryset.all(),
                            key=lambda row: getattr(row, self.field.group_by_field))
                ]
            for choice in self.field.choice_cache:
                yield choice
        else:
            for group, choices in groupby(self.queryset.all(),
                    key=lambda row: getattr(row, self.field.group_by_field)):
                yield (self.field.group_label(group), [self.choice(ch) for ch in choices])

More like this

  1. ModelChoiceField with option groups by anentropic 3 years, 4 months ago
  2. Changing queryset of a ModelChoiceField by mimog 3 years, 9 months ago
  3. Grouped Model Choice Field by JulieGoldberg 2 weeks, 2 days ago
  4. A Lazy ModelChoiceField implementation by alecdotico 1 year, 9 months ago
  5. CustomChoiceField, Selectable label field version of ModelChoiceField by mauro 7 years, 1 month ago

Comments

sorki (on March 8, 2011):

for this to work you have to:

  • from itertools import orderby
  • add queryset as the first parameter after self ('init' and then pass the queryset as the first parameter to the 'super' call (before *args).

Thank you the snippet (though it took me quiet a time to figure out the stuff mentioned above)

#

sorki (on March 16, 2011):
  • (from itertools import groupby)

#

Please login first before commenting.