[Answer]-Construct form from queryset

1👍

This is what the ModelMultipleChoiceField does. You can use it in conjunction with the CheckboxSelectMultiple widget to get what you want.

class MyForm(forms.Form):
    my_choices = forms.ModelMultipleChoiceField(
                      queryset=SomeModel.objects.all(),
                      widget=forms.CheckboxSelectMultiple)

Edit

To change the queryset depending on a value, you need to override __init__:

def __init__(self, *args, **kwargs):
    model_value = kwargs.pop('my_value', None)
    super(MyForm, self).__init__(*args, **kwargs)
    if model_value is not None:
        self.fields['my_choices'].queryset = SomeModel.objects.filter(my_field=my_value)

Leave a comment