UPDATE: Looks like I will have to create custom widget (Grouping CheckboxSelectMultiple Options in Django). Will include the final code when I'm done.
I have set up a choice field in a django form similar to another SO question.
However, I would like to make it a CheckboxSelectMultiple
widget, but doing so in the class Meta creates a checkbox for the entire group. Is this something I need to do in the __init__
, if so how?
class FooIterator(models.ModelChoiceIterator):
def __init__(self, *args, **kwargs):
super(models.ModelChoiceIterator, self).__init__(*args, **kwargs)
def __iter__(self):
for thing in MyModel.objects.all():
yield (thing.name, [(x.id, x.name) for x in MyModel2.objects.filter(name=thing.name)])
class MyForm(ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['myField'].choices = FooIterator()
for x in self.fields:
self.fields[x].widget.attrs['class'] = 'input-block-level'