gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:53:48 26 4
gpt4 key购买 nike

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

对于可能的答案,我需要“是/现在”、“1 到 5 中的 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) 的内容类型,以便仅显示答案类型?

最佳答案

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

模型.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
<小时/>

表单.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
<小时/>

View :

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

关于django - 为简单的调查应用程序生成表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/977856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com