form checkbox
Date: March 27th 2016
Last updated: March 27th 2016
You can add fields from a manytomany relationship in a form using ModelMultipleChoiceField. This field is set by default as a multiple select box (i.e. hold ctrl and click multiple objects). You can change the field to a checkbox by defining a widget.
forms.py
class FinsForm(forms.ModelForm):
fincompatability = forms.ModelMultipleChoiceField(
# provide a label
label = 'Fin compatability',
queryset = FinSystem.objects.all(),
widget = forms.CheckboxSelectMultiple)
class Meta:
model = Fin
fields = ['name',
'fintemplate',
'finremovable',
'finbase',
'findepth',
'finarea',
'finsweep',
'finsfoil',
'finsize',
'fincompatability',
'finnotes',]