2👍
✅
The initial
argument passes the initial values for the fields, not the available options for those fields. In your case you could tweak your EnglishTestForm.answer
field’s queryset dynamically:
class EnglishTestForm(forms.ModelForm):
class Meta:
model = EnglishTestAnswer
fields = '__all__'
widgets = {
'answer': forms.RadioSelect()
}
def __init__(self, *args, **kwargs):
super(EnglishTestForm, self).__init__(*args, **kwargs)
question = self.initial.get('question')
if question:
self.fields['answer'].queryset = question.Variants.all()
P.S.: Actually you have a security problem: a malicious user could forge your formset values, for example, making four forms with the same question id and all the possible answer ids. As you blindly save those forms, then, depending on your method of calculation, it may cause the false positive results of the test. Just to warn you.
Source:stackexchange.com