Django formsets

时间:2012-04-07 13:21:07

标签: django django-forms formsets

我正在用django制作调查网站。我是django的新手,所以如果我不能很好地解释,我会提前道歉。我的问题集中在以下模型:

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收到调查ID。通过调查ID,它获取调查类型并显示必须显示的问题。

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')

我已经阅读了关于formsets的django文档,但是我不明白我要在forms.py中写什么,所以我不能在views.py中调用表单来在显示问题的模板中呈现de form并在答案模型中写下答案。

提前致谢,对不起我的英语。

2 个答案:

答案 0 :(得分:1)

使用模型和foor循环解决。

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)

答案 1 :(得分:0)

您是否已将表单导入views.py?

之后,您只需创建一个将表单传递给模板的视图。

例如在views.py

def your_form(request):
    form = RegisterForm()
    return render_to_response('your_template.html', {'form': form}, RequestContext(request))

然后在您的模板中,您只需编写

即可呈现表单
{{ form }}