So just to explain what i’m trying to do.
I have a questionnaire which is made up of Questionnaire Model, Answer Model, Question Model, Respose Model
and now a Choices Model
all of which you basically did for me 
Each question in each questionnaire has different answers (Choices)
for which to choose.
When the Questionnaire is rendered all the questions are listed as select boxes.
I’ve updated the form and view to populate the questions related select box with the available choices, but at the moment i am stuck on how to filter each question to its choices as at the moment every select is displaying the choices from question 1 only.
Form.py
def __init__(self, *args, **kwargs):
question_id = kwargs.pop('question_id')
super().__init__(*args, **kwargs)
self.fields['answer'] = forms.ModelChoiceField(
queryset=Choice.objects.filter(question_id=question_id),
widget=forms.Select(attrs={'class': 'form-control select2'})
)
View.py
def get_questionnaire(request, project_id, questionnaire_id):
# Get or Create the Response object for the parameters
next = request.POST.get('next', '/')
response, created = ProjectQuestionnaireResponse.objects.get_or_create(
project_name_id=project_id, questionnaire_id=questionnaire_id
)
AnswerFormSet = modelformset_factory(ProjectQuestionnaireAnswer, form=AnswerForm, fields=('answer','notes',), extra=0)
answer_queryset = ProjectQuestionnaireAnswer.objects.filter(response=response
).order_by('question__sequence'
).select_related('question')
if request.method == 'POST':
formset = AnswerFormSet(request.POST, form_kwargs={'question_id': 1})
instances = formset.save()
return HttpResponseRedirect(next)
else:
# Get the list of questions for which no Answer exists
new_answers = ProjectQuestionnaireQuestion.objects.filter(
questionnaire__projectquestionnaireresponse=response
).exclude(
projectquestionnaireanswer__response=response
)
# This is safe to execute every time. If all answers exist, nothing happens
for new_answer in new_answers:
ProjectQuestionnaireAnswer(question=new_answer, response=response).save()
answer_formset = AnswerFormSet(queryset=answer_queryset, form_kwargs={'question_id': 1})
return render(request, 'project_evaluation.html', {'formset': answer_formset,'project_name':response.project_name,'title':response.questionnaire.title})
Now i know its this line that is the problem or at least one of possibly many problems.
formset = AnswerFormSet(request.POST, form_kwargs={'question_id': 1})
As this is returning the choices from question 1
only.
Is there a better way/correct way to be doing this?
I’m thinking that i need a for loop
in there somewhere so that the choices form part of the for loop
for each of the questions.
{% csrf_token %}
{{ form.non_field_errors }}
{{ formset.management_form }}
{% for form in formset %}
<p class="mg-b-10"><span class="tx-warning si si-question mr-1" data-toggle="tooltip" title="{{form.instance.question.description}}"></span> {{ form.id }} {{ form.instance.question.question }}<span class="tx-danger ml-2">*</span></p>
<div class="parsley-checkbox d-flex mg-b-0 mb-3" id="cbWrapper2">
{{ form.answer }}
</div><!-- parsley-checkbox -->
<div class="" id="cbErrorContainer2"></div>
{% endfor %}
Thanks again.
Tom.