Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 561
Darwin Tech Avatar asked Dec 04 '25 10:12

Darwin Tech


1 Answers

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

like image 125
orokusaki Avatar answered Dec 07 '25 04:12

orokusaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!