title: django saving two model on a oneto many relationship
body: <p>I have a template that I use to save two models on a one to many relationship. In this template, I have two questions the first question has radio choices and the second question has checkbox choices like what is shown on the image below. What happens is when I select, for instance, choice#1 in question 1 and check checkbox#1 in question 2 the form is saved successfully. But if I select choice#3 in question 1 and check checkbox#1 or checkbox#2 in question 2 then i get an error: CheckChoice matching query does not exist. I am not sure why i am getting this error.</p>
  <pre class="lang-python prettyprint-override"><code>class Survey(models.Model):
      title = models.CharField(max_length=200)
      created_at = models.DateTimeField(default=timezone.now)
      archive = models.CharField(max_length=200, default='')
  
      def __str__(self):
          return self.title
  
  
  class Question(models.Model):
      survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
      enter_question = models.CharField(max_length=900)
  
      def __str__(self):
          return self.enter_question
  
  
  class Choice(models.Model):
      question = models.ForeignKey(Question, on_delete=models.CASCADE)
      choice = models.CharField(max_length=100)
  
  
  class CheckChoice(models.Model):
      question = models.ForeignKey(Question, on_delete=models.CASCADE)
      choice_check = models.CharField(max_length=100)
  
  
  class SurveyAnswer(models.Model):
      orig_survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
  
  
  class QuestionAnswer(models.Model):
      answer = models.ForeignKey(Choice, on_delete=models.CASCADE)
      check_answer = models.ForeignKey(CheckChoice, on_delete=models.CASCADE)
      survey_answer = models.ForeignKey(SurveyAnswer, on_delete=models.CASCADE)
  </code></pre>
  <pre class="lang-python prettyprint-override"><code>def survey_fill(request):
      ans = SurveyAnswer()
      orig_survey = Survey.objects.get(id=request.POST['survey_id'])
      ans.orig_survey = orig_survey
      ans.save()
      questions = orig_survey.question_set.all()
      for question in questions:
          qc = request.POST['question' + str(question.id)]
          qa = QuestionAnswer()
          qa.answer = Choice.objects.get(id=int(qc))
          qa.check_answer = CheckChoice.objects.get(id=int(qc))
          qa.survey_answer = ans
          qa.save()
      ans.save()
      return render(request, 'survey-complete.html', {})
  </code></pre>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="d-flex flex-column"&gt;
      &lt;form method = "post" action ="{% url 'fill-survey' %}"&gt;
  
      &lt;input type = "hidden" name = "survey_id" value = {{survey.id}}&gt;
      {% csrf_token %}
      {% for question in questions %}
  
  
                  &lt;div class="p-2"&gt;
                      &lt;h4 class="header"&gt;
                      Question {{ forloop.counter }}: {{ question.enter_question }}
                  &lt;/h4&gt;
                  &lt;/div&gt;
  
          {% for choice in question.choice_set.all%}
  
  
               &lt;div class="p-2"&gt;
              &lt;input type="radio" name ="question{{question.id}}" value={{choice.id}}&gt;
                  {{choice.choice}}
               &lt;/div&gt;
  
              {% endfor %}
  
  
                  {% for choice in question.checkchoice_set.all%}
                      &lt;div class="p-2"&gt;
                          &lt;input type="checkbox" name ="question{{question.id}}" value={{choice.id}}&gt;
                          {{choice.choice_check}}
                      &lt;/div&gt;
                  {% endfor %}
  
          {% endfor %}
  
  
          &lt;div class="p-2"&gt;
              &lt;input type = "submit" value = "Complete!" class = "btn btn-lg btn-success"&gt;
          &lt;/div&gt;
      &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Updated view and template</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="d-flex flex-column"&gt;
      &lt;form method = "post" action ="{% url 'fill-survey' %}"&gt;
  
      &lt;input type = "hidden" name = "survey_id" value = {{survey.id}}&gt;
      {% csrf_token %}
      {% for question in questions %}
  
  
                  &lt;div class="p-2"&gt;
                      &lt;h4 class="header"&gt;
                      Question {{ forloop.counter }}: {{ question.enter_question }}
                  &lt;/h4&gt;
                  &lt;/div&gt;
  
          {% for choice in question.choice_set.all%}
  
  
               &lt;div class="p-2"&gt;
              &lt;input type="radio" name ="question{{question.id}}" value={{choice.id}}&gt;
                  {{choice.choice}}
               &lt;/div&gt;
  
              {% endfor %}
  
  
                  {% for choice in question.checkchoice_set.all%}
                      &lt;div class="p-2"&gt;
                          &lt;input type="checkbox" name ="checkbox{{question.id}}" value={{choice.id}}&gt;
                          {{choice.choice_check}}
                      &lt;/div&gt;
                  {% endfor %}
  
          {% endfor %}
  
  
          &lt;div class="p-2"&gt;
              &lt;input type = "submit" value = "Complete!" class = "btn btn-lg btn-success"&gt;
          &lt;/div&gt;
      &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>def survey_fill(request):
      ans = SurveyAnswer()
      orig_survey = Survey.objects.get(id=request.POST['survey_id'])
      ans.orig_survey = orig_survey
      ans.save()
      questions = orig_survey.question_set.all()
      for question in questions:
          if 'question' and 'checkbox' in request.POST:
              qc = request.POST['question' + str(question.id)]
              check = request.POST['checkbox' + str(question.id)]
              qa = QuestionAnswer()
              # qa1 = QuestionAnswer()
              qa.answer = Choice.objects.get(id=int(qc))
              qa.check_answer = CheckChoice.objects.get(id=int(check))
              qa.survey_answer = ans
              qa.save()
              # qa1.survey_answer = ans
              # qa1.save()
  
      ans.save()
      return render(request, 'survey-complete.html', {})</code></pre>
  </div>
  </div>
  </p>
  <p><a href="https://i.stack.imgur.com/rOA6G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rOA6G.png" alt="enter image description here" /></a></p>