无法从视图 Django 中设置表单选项
Unable to set form choices from view Django
我从 Django Form ChoiceField set choices in View in Form initial
那里学到了如何做到这一点
然而它似乎不能正常工作,没有给出选择
查看:
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 的对象,与表单正在创建的对象相同。
抱歉,如果我遗漏了一个重要的细节,我已经好几年没有在 Whosebug
上发帖了
默认情况下,模型表单创建一个 TextInput
来与 user_answer
字段一起工作(是一个没有选择的 models.CharField
),而 TextInput
字段不知道如何处理 choices
参数。
您可以尝试将选择直接分配给小部件:
form_class.fields['user_answer'].widget.choices = mc_choices
或将自定义字段添加到您的模型表单中:
class AskMCQuestionForm(forms.ModelForm):
user_answer = forms.ChoiceField(...)
我从 Django Form ChoiceField set choices in View in Form initial
那里学到了如何做到这一点然而它似乎不能正常工作,没有给出选择
查看:
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 的对象,与表单正在创建的对象相同。 抱歉,如果我遗漏了一个重要的细节,我已经好几年没有在 Whosebug
上发帖了默认情况下,模型表单创建一个 TextInput
来与 user_answer
字段一起工作(是一个没有选择的 models.CharField
),而 TextInput
字段不知道如何处理 choices
参数。
您可以尝试将选择直接分配给小部件:
form_class.fields['user_answer'].widget.choices = mc_choices
或将自定义字段添加到您的模型表单中:
class AskMCQuestionForm(forms.ModelForm):
user_answer = forms.ChoiceField(...)