Невозможно установить параметры формы из представления Django
Я узнал как это сделать из Django Form ChoiceField set choices in View in Form initial
Однако, похоже, это не работает правильно, выбор предоставляется
view:
form_class = AskMCQuestionForm()
mc_choices = []
if question.One:
mc_choices.append(tuple((1, question.One)))
if question.Two:
mc_choices.append(tuple((2, question.Two)))
if question.Three:
mc_choices.append(tuple((3, question.Three)))
if question.Four:
mc_choices.append(tuple((4, question.Four)))
if question.Five:
mc_choices.append(tuple((5, question.Five)))
mc_choices = tuple(mc_choices)
print(mc_choices)
form_class.fields['user_answer'].choices = mc_choices
form_class.fields['assignment'].initial = assignment
form_class.fields['exam'].initial = question.exam
form_class.fields['correct_answer'].initial = question.correct_answer
form_class.fields['text'].initial = question.text
print("About to render page for MC question")
return render(request, 'Exam/ask_question.html', {'question': question, 'form': form_class})
Форма:
class AskMCQuestionForm(forms.ModelForm):
class Meta:
model = MC_Question
fields = ('text', 'user_answer', 'assignment', 'correct_answer', 'exam',)
widgets = {
'text': forms.TextInput(attrs={'class': 'form-control', 'readonly': True}),
'user_answer': forms.Select(attrs={'class': 'form-control'}),
'assignment': forms.Select(attrs={'class': 'form-control'}),
'correct_answer': forms.HiddenInput(),
'exam': forms.HiddenInput(),
}
модель:
class MC_Question(models.Model):
One = models.CharField(max_length=200)
Two = models.CharField(max_length=200)
Three = models.CharField(max_length=200, blank=True, null=True)
Four = models.CharField(max_length=200, blank=True, null=True)
Five = models.CharField(max_length=200, blank=True, null=True)
class Answers(models.IntegerChoices):
one = 1
two = 2
three = 3
four = 4
five = 5
text = models.CharField(max_length=200)
correct_answer = models.IntegerField(choices=Answers.choices, blank=True, null=True)
user_answer = models.CharField(max_length=200)
exam = models.ForeignKey(Test, on_delete=models.CASCADE)
assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE, blank=True, null=True)
question - это объект MC_Question, тот же самый, что создается в форме. Прошу прощения, если я упустил какую-то важную деталь, прошло несколько лет с тех пор, как я писал на stackoverflow
По умолчанию модель формы создает TextInput
для работы с полем user_answer
(это models.CharField
без выбора), а поле TextInput
не знает, что делать с аргументом choices
.
Вы можете попробовать присвоить выбор непосредственно виджету:
form_class.fields['user_answer'].widget.choices = mc_choices
Или добавьте пользовательское поле в форму вашей модели:
class AskMCQuestionForm(forms.ModelForm):
user_answer = forms.ChoiceField(...)