Skip to content

Instantly share code, notes, and snippets.

@JulieGoldberg
Created April 9, 2015 00:25
Show Gist options
  • Save JulieGoldberg/dfc672ee39f4f1695547 to your computer and use it in GitHub Desktop.
Save JulieGoldberg/dfc672ee39f4f1695547 to your computer and use it in GitHub Desktop.
Display OptGroup options from a Django ModelChoiceField
from django import forms
class GroupedModelChoiceField(forms.ModelChoiceField):
def optgroup_from_instance(self, obj):
return ""
def __choice_from_instance__(self, obj):
return (obj.id, self.label_from_instance(obj))
def _get_choices(self):
if not self.queryset:
return []
all_choices = []
if self.empty_label:
current_optgroup = ""
current_optgroup_choices = [("", self.empty_label)]
else:
current_optgroup = self.optgroup_from_instance(self.queryset[0])
current_optgroup_choices = []
for item in self.queryset:
optgroup_from_instance = self.optgroup_from_instance(item)
if current_optgroup != optgroup_from_instance:
all_choices.append((current_optgroup, current_optgroup_choices))
current_optgroup_choices = []
current_optgroup = optgroup_from_instance
current_optgroup_choices.append(self.__choice_from_instance__(item))
all_choices.append((current_optgroup, current_optgroup_choices))
return all_choices
choices = property(_get_choices, forms.ChoiceField._set_choices)
class ExampleChoiceField(GroupedModelChoiceField):
def optgroup_from_instance(self, obj):
return obj.group.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment