[Fixed]-How to supply choices to a formset, if choices come from queryset or other view logic?

1👍

From the official documentation

class MCQuestionAnswerForm(forms.Form):
     question = forms.CharField()
     mcq_answer_choice = forms.ChoiceField(widget=forms.RadioSelect)

     def __init__(self, *args, **kwargs):
         self.extra = kwargs.pop('extra')
         super(MyArticleForm, self).__init__(*args, **kwargs)

         # You have now use the value of self.extra to construct or alter your form body
         # For example:
         self.fields['mcq_answer_choice'].initial = self.extra

MCQuestionAnswerFormSet = formset_factory(MCQuestionAnswerForm, extra=0)

Leave a comment