title: Django survey with forms
body: <p>I am building a survey tool and I'm wondering how would I continue with this or if my current solution is even the proper way to do it. </p>
  
  <ol>
  <li><p>Admin of the page may add or remove questions from questionnaires, so if I have understood it, I can't use ModelForms to handle the form data?</p></li>
  <li><p>A form may consist of 5 multichoice questions and 2 free text questions or any other amount of different types of questions so there isn't any fixed type of questionnaire</p></li>
  <li><p>How do I then save the values of the form as I do not have a model to use?</p></li>
  </ol>
  
  <p>Is this even possible to achieve without using a model for the form?</p>
  
  <p>Thank you for any input in advance.</p>
  
  <hr>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.shortcuts import render
  from .models import Questionnaire, Question, Answer
  
  def index(request):
      all_questionnaires = Questionnaire.objects.all()
      all_questions = Question.objects.all()
      return render(request, 'questions/index.html', locals())
  
  def questionnaire_detail(request, questionnaire_id):
      questionnaire = Questionnaire.objects.get(id=questionnaire_id)
      questions = Question.objects.filter(questionnaire=questionnaire)
      return render(request, 'questions/questionnaire.html',
                    {'questionnaire': questionnaire, 'questions': questions})
  
  def calculate(request):
      if request.method == 'POST':
          pass
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.db import models
  
  MC = 'MC'
  CN = 'CN'
  TX = 'TX'
  
  CATEGORY = (
      (MC, 'Multichoice'),
      (CN, 'Choose N'),
      (TX, 'Text'),
  )
  
  VALYK = '1'
  VALKA = '2'
  VALKO = '3'
  VALNE = '4'
  VALVI = '5'
  
  MULTICHOICE = (
      (VALYK, 'Least'),
      (VALKA, 'Less than average'),
      (VALKO, 'Average'),
      (VALNE, 'More than average'),
      (VALVI, 'Most'),
  )
  
  class Questionnaire(models.Model):
      questionnaire_name = models.CharField(max_length=100,
                                            verbose_name="Questionnaire",
                                            null=False,
                                            default=None,
                                            blank=False)
      def __str__(self):
          return self.questionnaire_name
  
  class Question(models.Model):
      questionnaire = models.ManyToManyField(Questionnaire)
      question_text = models.CharField(max_length=200,
                                       verbose_name="Questionnaire name",
                                       null=True,
                                       default=None,
                                       blank=True)
      question_category = models.CharField(max_length=2,
                                           verbose_name="Question category",
                                           null=False,
                                           choices=CATEGORY,
                                           default=None,
                                           blank=False)
      def __str__(self):
          return self.question_text
  
  class Answer(models.Model):
      question = models.ForeignKey(Question)
  
  class MultiChoiceAnswer(Answer):
      answer = models.IntegerField(choices=MULTICHOICE)
      def __str__(self):
          return self.answer
  </code></pre>
  
  <p><strong>questionnaire.html</strong></p>
  
  <pre><code>{% extends "questions/base.html" %}
  {% block title_html %}
      Questionnaire
  {% endblock %}
  {% block h1 %}
      Questionnaire
  {% endblock %}
  {% block content %}
      {% if questions|length &gt; 0 %}
          &lt;form action="{% url "questions:calculate" %}" method="post"&gt;
              {% csrf_token %}
              {% for question in questions %}
                  {{ question.question_text }}&lt;br&gt;                
                  {% if question.question_category == "MC" %}
                      &lt;input type="radio" name="{{ question.id }}" value="1"&gt; 1&lt;br&gt;
                      &lt;input type="radio" name="{{ question.id }}" value="2"&gt; 2&lt;br&gt;
                      &lt;input type="radio" name="{{ question.id }}" value="3"&gt; 3&lt;br&gt;
                      &lt;input type="radio" name="{{ question.id }}" value="4"&gt; 4&lt;br&gt;
                      &lt;input type="radio" name="{{ question.id }}" value="5"&gt; 5&lt;br&gt;
                  {% elif question.question_category == "CN" %}
                      &lt;input type="checkbox" name="{{ question.id }}" value="a"&gt;a&lt;br&gt;
                      &lt;input type="checkbox" name="{{ question.id }}" value="b"&gt;b&lt;br&gt;
                  {% elif question.question_category == "TX" %}
                      &lt;textarea rows="4" cols="50" name="{{ question.id }}"&gt;Test&lt;/textarea&gt;&lt;br&gt;
                  {% endif %}
              {% endfor %}    
          &lt;input type="submit" value="Send" /&gt;
          &lt;/form&gt;                 
      {% else %}
      &lt;span&gt;No questions&lt;/span&gt;
      {% endif %}             
  {% endblock %}
  </code></pre>