whitebear
whitebear

Reputation: 12461

Use MultipleChoiceField for class

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.

https://docs.djangoproject.com/en/4.0/ref/forms/fields/

However there are only this, not mentioned about using class instance.

enter image description here

Upvotes: 1

Views: 192

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

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'
    )

Upvotes: 2

Related Questions