0

I have the three following models:

class AnswerItem(models.Model):
    item_name = models.CharField(max_length=320)
    item_number = models.PositiveIntegerField()

class AnswerMenu(models.Model):
    menu_name = models.CharField(max_length=320)
    answer_item = models.ForeignKey(AnswerItem)

class QuestionAnswer(models.Model):
    answer = models.ForeignKey(AnswerMenu)
    answer_item = models.ForeignKey(AnswerItem)

Answer menus are currently displayed on a single page using a list view, with the following template:

{% for answer_menu in answer_menus %}
    <div>{{ answer_menu.menu_name }}</div>
    {% for answer_item in answer_menu %}
        <p>
          <label>
            <input id="{{ answer_item.pk }}" name="{{ answer_menu.pk }}" type="radio">
          </label>
        </p>

Now, the trouble I have is I would like to create a single form to save all selected answers using the radio buttons on the page. Since there are multiple answer menus shown on the page, posting through this form would create several QuestionAnswer items.

How would you approach this?

Greg
  • 3
  • 1
  • Does this answer your question? [Django: multiple models in one template using forms](https://stackoverflow.com/questions/569468/django-multiple-models-in-one-template-using-forms) – Zkh Oct 24 '22 at 23:00
  • I would Submit the Form with AJAX & Jquery, because I can pack the form data in a complex way like: `JSON.stringify([{'item':0, 'field0':val0}, {'item':0, 'field0':val1}])` .. Then in the view I would do `json.loads(request.POST.get('formdata'))` - loop through that like a normal array of dictionaries and create each item! This isn't a very Django-y method though – Nealium Oct 25 '22 at 00:34

0 Answers0