0

A requirement for my app is to give user's capability to create a survey. For each survey, the user should have the capability to add any number of questions. I am trying to achieve this by first defining my models and a form.

# models.py.
from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)

    def __str__(self):
        return self.question_text


class Survey(models.Model):
    survey_name = models.CharField(max_length=200)
    questions = models.ForeignKey(Question, on_delete=models.CASCADE)

    def __str__(self):
        return self.survey_name
#forms.py
from django import forms

class Survey(forms.Form):
    survey_name = forms.CharField(required=200)
    #TODO: define questions

I am stuck. In my form module, how do I define the one to many relationship between the survey and questions in order for the user to add and define questions for each survey they create.

walter_dane
  • 115
  • 4
  • 8

1 Answers1

0

You can follow this tutorial to make dynamic forms dynamic form using django or you can use javascript dynamic input using javascript to let user creat as many field wish. and get those field on view using request.POST.getlist("html input field name") and then insert onto model.