Hi everyone! I have a doubt about using formset with class-based views, that may be simpler, but i couldn’t figured out so far.
I have the following models:
class ProjectModel(models.Model):
charcode = models.Charfield(unique=True)
class QuestionModel(models.Model):
title = models.Charfield()
type = models.CharField(choices=('text','rate'))
class SurveyModel(models.Model):
project = models.ForeignKey(ProjectModel)
active = models.BooleanField(default=False)
questions = models.ManyToManyField(QuestionModel)
class AnswerModel(models.Model):
question = models.ForeignKey(QuestionModel)
response_text = models.TextField()
response_rate = models.IntegerField(choices=(1,2,3,4,5))
class SubmissionModel(models.Model):
user = models.ForeignKey(get_user_model())
survey = models.ForeignKey(SurveyModel)
answers = models.ManyToManyField(AnswerModel)
I need a view to render **questions**
of a active **survey**
so a user can fill it in a form that will create **answers**
objects and a **submission**
object.
so, I have this url:
path('submission/<str:charcode>/, SubmissionView.as_view())
this view that receives **charcode**
from url:
class SubmissionView(CreateView):
form_class = SubmissionFormSet
template_name='create_submission.html'
and this formset (in construction):
class SubmissionFormSet(form.BaseFormSet):
# need to get questions from survey_objects.get(project__charcode = 'charcode', active=True)
# define each field based on question type to properly widget choice
# save answers and submission based on responses from user
i really have no idea yet, but i guess i must use formset_factory, maybe inside view like **AnswerFormset = formset_factory(AnswerForm, formset=SubmissionFormSet)**
but not sure to be honest