Select a valid choice ModelChoiceField

2024/10/16 2:23:35

Whenever im running form.is_valid() i get the error:

Select a valid choice. That choice is not one of the availablechoices.

Here is what I do in my view:

timeframes = HostTimeFrame.objects.all()
if request.method == 'POST':form = SelectDatesForm(request.POST, timeframes=timeframes)if form.is_valid():pass
else:form = SelectDatesForm(timeframes=timeframes)

My form does this:

 class SelectDatesForm(forms.Form):timeframes = forms.ModelChoiceField(queryset=HostTimeFrame.objects.none(), widget=forms.CheckboxSelectMultiple,empty_label=None)def __init__(self, *args, **kwargs):qs = kwargs.pop('timeframes')super(SelectDatesForm, self).__init__(*args, **kwargs)self.fields['timeframes'].queryset = qs.order_by('start')

Ive been trying for hours to find where this actual validation is done, and i found it, created a bug here.

Answer

According to documentation ModelChoiceField its Default widget is Select doc

If you want to select multiple values you must use ModelMultipleChoiceField like this :

timeframes = forms.ModelMultipleChoiceField(queryset=HostTimeFrame.objects.none(), widget=forms.CheckboxSelectMultiple,empty_label=None)
https://en.xdnf.cn/q/117762.html

Related Q&A

Let a module file use a global variable?

Forgive me if this is just a super easy solution as I am pretty new to Python. Right now Im trying to make a basic video game, and to save space I decided to make a module for a combat encounter -- so …

Python Subprocess readline hangs() after reading all input

I am trying to readlines from the tcp server that I ran in the same script. I am able to send one command and reads its output but at the end (after reading all outputs) it looks like that program hang…

Python Counting countries in dictionary

Im writing a function that counts the number of times a country appears in a dictionary and returns the country that appeared the most. If more then one country appears the most then it should return a…

How to wait for any socket to have data?

Im implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.Im not sur…

Retrieving ad URLs

Im looking for a way to retrieve the ad URLs for this website. http://www.quiltingboard.com/resources/What I want to do is probably write a script to continuously refresh the page and grab the ad URLs.…

Extract data (likes) from JSON API using Python

I want to pull the number of likes for my project. Heres my code: import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf…

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…

List of tuples to nested dictionary without overriding

I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python[(a, 1),(b, true),(b, none),(a, 2),(b, true),(a, 3),(b, false)]{a: {1 : { b : (true,none)…