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

Vik*_*ica 6 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
Run Code Online (Sandbox Code Playgroud)

有没有(希望:简单)方法通过循环所有问题并创建与问题的answer_type兼容的答案形式来生成表单?

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

Vik*_*ica 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

风景:

#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,

    })  
Run Code Online (Sandbox Code Playgroud)

并在模板中:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

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

编辑

内容类型过滤可以像

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'))
Run Code Online (Sandbox Code Playgroud)