Stop forms from requiring a manytomany selection

Date: March 29th 2016
Last updated: March 29th 2016

I have recently found an issue with my forms relating to a manytomany relationship. The problem was that an object instance could start out with having no selection from another table. The moment that I did select an object to relate the two tables together I could never go back to selecting nothing. In my example, a surfer (table/model) could select a board (table/model), but could not select None. This was solved by adding required=False to the queryset.

forms.py

class BoardSelectForm(forms.ModelForm):
    boards = forms.ModelMultipleChoiceField(
            queryset=Board.objects.all(),
            ############ Solution ###############
            required=False  #<---- add this line
            ) 
            #####################################

    class Meta:
        model = Surfer
        fields = ['boards']

results matching ""

    No results matching ""