Probems creating a MultipleSelect form

I’ve tried to create a form, to view and select multiple choices, to delete them.

I’ve tried to make this with ModelForm, but was unable to get a good way to do it.

With forms.Forms I did this :

class DeleteQuotesForm(forms.Form):
    Choices = Quote.objects.values('id')
    picked = forms.MultipleChoiceField(choices=Choices, widget=forms.CheckboxSelectMultiple)

I chosed objects.values(‘id’) because using objects.all() I got an error saying the values where unpackable. Maybe because it give a QuerySet, and the forms do not manage it in the template ?

Now, I get a ValueError saying “not enough values to unpack (expected 2, got 1)” … it seems to be linked with the “return” line of the view :

def suppression_quote(request):
    if request.method == 'POST':
        form = DeleteQuotesForm(request.POST)
        form = DeleteQuotesForm
    return render(request, 'Citatio/suppression_quote.html', {'form':form})

Also, no sure of the method I use to get select value to delete them (Quote.objects.filter(form.picked).delete()

Here is the template suppression_quote.html :

{% block content %}
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
        <button type="submit">Enregistrer</button>
{% endblock %}

Thanks for your lights :slight_smile:

The choices parameter for a ChoiceField (and MultipleChoiceField) specifies what you need to supply.

Also see the choices docs on the fields page for more details.


Thank you. So I only had to add a parameter to objects.value().

But I got labels instead of real values, so it was weird. I figured I had to use values_list() instead of values(), because choices() waits for a tuple, and values() renders a dict, whereas values_list() renders a tuple.

Got it !

Thank you :slight_smile:

1 Like