为简单的调查应用程序生成表单

时间:2009-06-10 20:03:56

标签: django django-forms

我正在编写一个简单调查的应用程序。

对于可能的答案,我需要“是/现在”,“1到1中的1”和短文

在管理员中,它应该是可选择的,应该给出什么样的答案。

我的模特:

from django.db import models
from django.contrib.contenttypes.models import ContentType

CHOICES=((1,'excactly true'),(2,'mostly true'),(3,'mostly untrue'),(4,'untrue'),(5,'I don\'t know '))
class Answer(models.Model):
    question = models.ForeignKey("Question")

class ChoiceAnswer(Answer):
    answer = models.IntegerField(max_length=1, choices=CHOICES)
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class TextAnswer(Answer):
    answer= models.CharField(max_length=255)
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class BooleanAnswer(Answer):
    answer= models.BooleanField(choices=((True,'yes'),(False,'no')))
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class Question(models.Model):
    question = models.CharField(max_length=255)
    answer_type = models.ForeignKey(ContentType)

    def __unicode__(self):
        return u'%s'%self.question

通过循环遍历所有问题并创建与问题的answer_type兼容的答案形式,是否存在(希望:简单)生成表单的方式?

是否可以过滤answer_type = models.ForeignKey(ContentType)的内容类型,以便只显示answertype?

1 个答案:

答案 0 :(得分:11)

当我自己发现解决方案时,我会回答我自己的问题:

models.py:

CHOICES=((1,'exactly true'),(2,'mostly true'),(3,'mostly untrue'),(4,'untrue'),(5,'I don\'t know '))

class Answer(models.Model):
    question = models.ForeignKey("Question")

class ChoiceAnswer(Answer):
    answer = models.IntegerField(max_length=1, choices=CHOICES)
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class TextAnswer(Answer):
    answer= models.TextField()
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class BooleanAnswer(Answer):
    answer= models.BooleanField(choices=((True,'yes'),(False,'no')))
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class Question(models.Model):
    question = models.CharField(max_length=255)
    answer_type = models.ForeignKey(ContentType)

    def __unicode__(self):
        return u'%s'%self.question

forms.py:

class ChoiceAnswerForm(forms.ModelForm):
    class Meta:
        model = ChoiceAnswer
        exclude=("question",)
ChoiceAnswer.form = ChoiceAnswerForm

class BooleanAnswerForm(forms.ModelForm):
    class Meta:
        model = BooleanAnswer
        exclude=("question",)
BooleanAnswer.form= BooleanAnswerForm

class TextAnswerForm(forms.ModelForm):
    class Meta:
        model = TextAnswer
        exclude=("question",)
TextAnswer.form = TextAnswerForm

观点:

#needed for monkey-patching.
from survey.forms import BooleanAnswerForm, TextAnswerForm, ChoiceAnswerForm 

def index(request):
    questions = Question.objects.all() 
    if request.method == 'POST': # If the form has been submitted...
        print request.POST
        for q in questions :
            try:
                data ={ u'%s-answer'%q.id: request.POST[u'%s-answer'%q.id]}
            except:
                data = { u'%s-answer'%q.id: None}
            q.form = q.answer_type.model_class().form(prefix="%s"%q.id, data=data)    
    else:
        for q in questions :
            q.form = q.answer_type.model_class().form(prefix="%s"%q.id) 

    return render_to_response('survey.html', {
        'questions': questions,

    })  

并在模板中:

{% block content %}

    <div class="survey">
        <form enctype="multipart/*" action="/" method="post">

        {% for question in questions %}
            <p>{{ question.question }}</p><ul>{{ question.form.as_ul }}</ul>
        {% endfor %}

    <div><input type="submit" value="submit" /></div>
</form>
</div>

{% endblock %}

通过某种注册可以避免猴子修补。但是现在我很好。

修改

内容类型过滤可以像

那样完成
answer_type = models.ForeignKey(ContentType, 
              limit_choices_to = Q(name='text answer', app_label='survey')| \
                                 Q(name='boolean answer', app_label='survey')| \
                                 Q(name='choice answer', app_label='survey'))