Search code examples
djangodjango-formsformsets

Django formsets


I'm making a survey site with django. I am pretty newbie with django so I apologize in advance if I can not explain well. My question focuses on the following models:

class SurveyType(models.Model):

    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name    

class Question(models.Model):

    ANSWERTYPE_CHOICES = (
        (u'T', u'Text'),
        (u'R', u'Range'),
        (u'M', u'Media'),
    )

    question = models.CharField(max_length=200)

    surveytype = models.ForeignKey(SurveyType)

    answertype = models.CharField(max_length=1, choices=ANSWERTYPE_CHOICES)

    order = models.IntegerField(default=0)

    def __unicode__(self):
        return self.question        


class Survey(models.Model):

    course = models.ForeignKey(Course)

    surveytype = models.ForeignKey(SurveyType)

    def __unicode__(self):
        return u"%s %s" % (self.course, self.surveytype) 

class Answer(models.Model):

    answer = models.CharField(max_length=400)

    survey = models.ForeignKey(Survey)

    question = models.ForeignKey(Question)

    def __unicode__(self):
        return self.answer

Django receives survey id. With the survey id it gets the surveytype and shows questions that must be displayed.

def survey(request, survey_id):
    survey_data = get_object_or_404(Survey, pk=survey_id)
    survey_type = survey_data.surveytype
    questions = Question.objects.all().filter(surveytype = survey_type).order_by('order')

I have read the django documentation about formsets but I don't understand what I have to write in forms.py, so I can't call the form in views.py to render de form in the template that shows the questions and write the answers in the answer model.

Thanks in advance and sorry for my english.


Solution

  • Solved using modelforms and a foor loop.

    Models.py

    class Question(models.Model): 
        question = models.CharField(max_length=200)
        surveytype = models.ForeignKey(SurveyType)
        answertype = models.ForeignKey(ContentType,
        limit_choices_to = Q(name='text answer', app_label='surveys')| \
        Q(name='media answer', app_label='surveys')| \
        Q(name='range answer', app_label='surveys'))
    
    class RangeAnswer(models.Model):
        answer = models.IntegerField(max_length=1, choices=CHOICES_RANGE, default=0)
        def __unicode__(self):
            return u'%s'%(self.answer)
    
    class TextAnswer(models.Model):
        answer= models.CharField(max_length=200)
        def __unicode__(self):
            return u'%s'%(self.answer)
    
    class MediaAnswer(models.Model):
        answer= models.ForeignKey(Media)
        def __unicode__(self):
            return u'%s'%(self.answer)
    

    Forms.py

    class RangeAnswerForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(RangeAnswerForm, self).__init__(*args, **kwargs)
            self.fields['answer'].label = "Mi valoración"    
        class Meta:
            model = RangeAnswer
            widgets = {
                'answer': forms.RadioSelect(renderer=RadioRenderer)
            } 
    RangeAnswer.form = RangeAnswerForm
    
    class MediaAnswerForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(MediaAnswerForm, self).__init__(*args, **kwargs)
            self.fields['answer'].label = "Medio"    
        class Meta:
            model = MediaAnswer
    MediaAnswer.form= MediaAnswerForm
    
    class TextAnswerForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(TextAnswerForm, self).__init__(*args, **kwargs)
            self.fields['answer'].label = "Respuesta"    
        class Meta:
            model = TextAnswer
    TextAnswer.form = TextAnswerForm
    

    Views.py

    for q in questions :
        q.form = q.answertype.model_class().form(prefix="%s"%q.id)