2012-04-07 69 views
0

我正在用django製作一個調查網站。我是django的新手,所以如果我解釋不好,我會提前道歉。我的問題主要關注以下型號:Django formsets

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

我看了一下表單集Django的文檔,但我不明白我在forms.py寫的,所以我不能調用views.py的形式來呈現德形式的模板顯示問題並在答案模型中編寫答案。

在此先感謝和抱歉我的英語。

回答

1

解決使用modelforms和福爾循環。

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

您是否已將表單導入到views.py?

之後,您只需創建一個將窗體傳遞給模板的視圖。

例如在views.py

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

,然後在你的模板就可以呈現形式簡單地寫

{{ form }} 
+0

是的,但問題是,我不知道在寫形式的.py。 Django文檔顯示如何使用使用一個或兩個模型的表單,而不是四個:S。我的問題是創建form.py – 2012-04-07 14:07:18

相關問題