[Django]-Django Form | Group Model | MultiSelect | getting only single value from the form by using MultiSelect widget

3πŸ‘

βœ…

If you access request.POST.get('key') (or request.POST['key']), you only get the last value associated with that key.

You access all values with the .getlist(…) methodΒ [Django-doc]:

print(request.POST.getlist('the_choices'))

But normally you process data with the form itself, so:

form = MyForm(request.POST, request.FILES)
if form.is_valid():
    print(form.cleaned_data['the_choices'])

This will also clean the data and return model objects, not their primary keys.

1πŸ‘

use getlist

https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.QueryDict.getlist

try this

the_choices = request.POST.getlist('the_choices')
πŸ‘€rahul.m

Leave a comment