[Answered ]-Retrieving models from form with ModelMultipleChoiceField

2👍

In add_works, you’re not constructing your WorkSelectForm the right way. It’s expecting as a first parameter the queryset of possible/authorized choices, then the POST data.

Also, you’re not accessing the selected works correctly from the form. You have to use is_valid method on the form, then use cleaned_data as described in the doc.

From what I see in your work_search view, there’s no restriction on which Work objects you can search then add to the result, so you could do simply:

def add_works(request):
    #if request.method == POST:
    form = WorkSelectForm(Work.objects.all(), request.POST)
    if form.is_valid():
        # the items are in form.cleaned_data['works']
        items = form.cleaned_data['works']
        return render_to_response("add_works.html", {"items":items})
    else:
       # handle error case here
       ...

Leave a comment