Select A Valid Choice. 6da87c51321849bdb7da80990fdab19b Is Not One Of The Available Choices
Solution 1:
Solved it , so basically when ever i used to submit my form a post request was recreated so i was basically thinking that if user have filled all the required details properly in form then it would directly go to the form_valid()
method but i was wrong before going to the form valid method it would initialize my __init__
method of Start_Game class in views after that it would initialize my get_form_kwargs()
so in this get_form_kwargs()
i have written code to generate the nex question and there options to send it to my form in forms.py so this would directly update my multichoice option in forms with next questions option and after updating multichoice option it would then compare my previously selected options value with the newly updated values so obviously this created the above error as the selected option was not available in the updated options so basically i wrote down an if condition in the below code to check if the request was post or get
defget_form(self,*args, **kwargs):
if self.request.method == 'GET':
self.question_and_type.clear()
self.unanswered_question = UserAnswer.get_unanswered_question(self.request.user.UserId,
Quiz.objects.get(category = self.slug).quizId)
self.question = self.unanswered_question.first()
self.type = self.question.type
self.question_and_type.extend([self.question, self.type])
return self.form_class(**self.get_form_kwargs())
else :
form = AnswerForm(question = self.question_and_type[0],
type = self.question_and_type[1],
data = self.request.POST)
return form
defget_form_kwargs(self):
if self.request.method == 'GET' :
kwargs = super().get_form_kwargs()
returndict(kwargs,question=self.question,type=self.type)
so now i would only get the new questions if request was "GET" else if it was "POST" then i would simply send the data of current question to the form
form = AnswerForm(question = self.question_and_type[0],
type = self.question_and_type[1],
data = self.request.POST)
so that it can successfully validate if selected option was present in the available options. FYI i'm storing previous questions in self.question_and_type
while 'GET' request is made . You can check my github for more details
GITHUB_HERE
I think so this is not an efficient way but it worked for me and If there is another good way then feel free to comment :)
Post a Comment for "Select A Valid Choice. 6da87c51321849bdb7da80990fdab19b Is Not One Of The Available Choices"