简体   繁体   中英

MultipleChoiceField - invalid_choice error - Select a valid choice. SomeChoice is not one of the available choices

I am creating a form in django, hoping to allow users to delete some of their devices. When I click on the submit button of my form, I keep getting the message: Select a valid choice. <Some choice> is not one of the available choices Select a valid choice. <Some choice> is not one of the available choices . Here is my code. Thanks a lot :)

forms.py

class DeleteDeviceForm(forms.Form):
    devices = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)

views.py

def delete_device(request):
    if request.method == 'POST':
        deletedeviceform = DeleteDeviceForm(request.POST)
        if deletedeviceform.is_valid():
            devicelist = request.POST.getlist('devices')
#will put other stuff there to process the data later, just want to access list now
            return HttpResponseRedirect('/accounts/loggedin', {"devicelist": devicelist, })

    else: #if not a POST request 
        userid = request.user.profile.pk
        devices = Device.objects.filter(user_id=userid)
        deletedeviceform = DeleteDeviceForm()
        deletedeviceform.fields['devices'].choices = [(x.id, x) for x in devices]

    return render(request, 'userprofile/delete_device.html', {"full_name": request.user.username, "deletedeviceform": deletedeviceform,})

Note that: I don't have a model for this form

You've set the list of valid choices on the GET request only. On the POST, there are no choices, so the field can never be valid.

That code should go in the form's __init__ method, so it is run every time the form is instantiated.

Thanks to @Daniel Roseman, I was able to figure it out.

Here is how I changed my code :

forms.py

class DeleteDeviceForm(forms.Form):
    devices = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,label="Select the devices you want to delete:")

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(DeleteDeviceForm, self).__init__(*args, **kwargs)
        self.fields['devices'].choices = [(x.id, x) for x in Device.objects.filter(user_id=user)]

views.py

changed only one line to :

deletedeviceform = DeleteDeviceForm(request.POST, user=request.user.profile.pk)

You can use very simple way in which you just have to update model not form. Use django-multiselectfield

pip install django-multiselectfield

Check here for reference https://pypi.org/project/django-multiselectfield/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM