Home > Software design >  Use MultipleChoiceField for class
Use MultipleChoiceField for class

Time:02-16

I want to use multipleChoiceField, to choice from model

I have model,Template so ,I did this in forms.py

class WorkerForm(forms.ModelForm):
    templates = forms.MultipleChoiceField(
        Template.objects.all(), required=False,  label='template')

However it shows error

    templates = forms.MultipleChoiceField(
TypeError: __init__() takes 1 positional argument but 2 were given

I am checking the document here.

enter image description here

CodePudding user response:

You should use a ModelMultipleChoiceField [Django-doc], and probably it is better to work with a named parameter:

class WorkerForm(forms.ModelForm):
    templates = forms.ModelMultipleChoiceField(
        queryset=Template.objects.all(), required=False,  label='template'
    )
  • Related