Darwin Tech
Darwin Tech

Reputation: 18929

Django multiple select form - TypeError: int() argument must be a string or a number, not 'list'

I have a form like so:

class PartnerProductsForm(forms.Form):
    product = forms.ModelChoiceField(
        queryset=Product.objects.all(),
        widget=forms.CheckboxSelectMultiple(
            attrs={"checked": ""}
        ),
        empty_label=None,
    )

And my views:

...
product_form = PartnerProductsForm(request.POST or None)
    if product_form.is_valid():
        # do stuff

But when I submit I get the following error:

TypeError: int() argument must be a string or a number, not 'list'

It seems like the form validation is expecting an int, but of course I will be retuning a list of the checked options. How am I supposed to deal with this?

Upvotes: 2

Views: 1391

Answers (1)

orokusaki
orokusaki

Reputation: 57128

The form field you're looking for is ModelMultipleChoiceField, rather than ModelChoiceField.

Upvotes: 11

Related Questions