ModelChoiceField: Выберите правильный выбор. Это не один из доступных вариантов. Только если ответ неверный
У меня есть поле ModelChoice, которое содержит 4 определения словарных слов в качестве вариантов для "what is the definition of __". При отправке формы я получаю: "Выберите правильный вариант. Этот вариант не является одним из доступных вариантов", но это происходит только в том случае, если выбранный ответ неверен. В противном случае все работает нормально.
Forms.py
class Questions(forms.Form):
choice = forms.ModelChoiceField(
widget=forms.RadioSelect,
queryset=QuesModel.objects.all(),
to_field_name='display',
initial = 0
)
Models.py
class QuesModel(models.Model):
display = models.CharField(max_length=200,null=True)
def __str__(self):
return self.display
views.py
deck = Word.objects.all()
deck_list = list(Word.objects.all())
cards = deck.filter(xdef = 0)
options = []
options.append(cards[0])
random.shuffle(deck_list)
options.extend([deck_list[1], deck_list[2], deck_list[3]])
[enter image description here][1]random.shuffle(options)
QuesModel.objects.filter(id = 1).update(display = 'adjective')
QuesModel.objects.filter(id = 2).update(display = 'noun')
QuesModel.objects.filter(id = 3).update(display = 'verb')
QuesModel.objects.filter(id = 4).update(display = 'adverb')
q = "What part of speech is " + cards[0].name + "?"
if request.method == "POST":
form = Questions(request.POST)
if form.is_valid():
i = form.cleaned_data.get('choice') #is object
input = i.display
answer = cards[0].pos
print(input)
print(answer)
if (input == answer):
print('correct')
print('updating: ' + cards[0].name)
Word.objects.filter(name = cards[0].name).update(xpos = 1)
messages.info(request, answer + " is correct!")
return redirect("learn")
else:
print('wrong')
print('updating: ' + cards[0].name)
Word.objects.filter(name = cards[0].name).update(xpos = 2)
messages.info(request, "The correct answer was: " + answer)
return redirect("learn")
else:
form = Questions()
Counter.objects.filter(id = 1).update(step = s+1)
print(counter.step)
return