I am working on English language test application. There are questions and 4 possible answers for each question. Only one answer is correct. Here are the models:
class EnglishTestQuestion(AbstractDatetimeModel):
text = models.CharField(max_length=255, verbose_name=u"Question text")
class EnglishTestAnswerVariants(models.Model):
question = models.ForeignKey(EnglishTestQuestion, verbose_name=_(u"question"), related_name='Variants')
text = models.CharField(max_length=255, verbose_name=u"Text")
is_correct = models.BooleanField(_(u"Is correct"), default=False)
class EnglishTestAnswer(AbstractDatetimeModel):
user = models.ForeignKey(User, blank=True, null=True, verbose_name=u"user")
question = models.ForeignKey(EnglishTestQuestion, blank=False, verbose_name=u"question")
answer = models.ForeignKey(EnglishTestAnswerVariants, verbose_name=u"answer")
Here is the view. I create a form for each question and pass initial data. Question field gets initial data, but Answer field ignores it and displays all answers,:
def english_test(request):
questions = EnglishTestQuestion.objects.all()
QuestionFormSet = formset_factory(form=EnglishTestForm, max_num=questions.count())
formset = QuestionFormSet(initial=[{
'question': x,
'answer': EnglishTestAnswerVariants.objects.filter(question=x)
} for x in questions])
return render(request, 'testing/english_test.html', {"questions": questions, "question_formset": formset})
But it should display for each question only those answers which connected via Foreign Key.
Here is form:
class EnglishTestForm(forms.ModelForm):
class Meta:
model = EnglishTestAnswer
fields = '__all__'
widgets = {
'answer': forms.RadioSelect()
}
How to pass in Answer field only those answers, which are connected with question by FK?