我一直在学习django几年,并认为自己是一个高级初学者,哈。我正在为一位客户制作一份“加权调查问卷”(这是我能给它的最好的名字)并且已经开始讨论去哪儿了。
客户提出了一系列是或否的问题,根据答案,将适用某些加权。在提交结束时,我需要检查答案并加权,而不是根据权重显示包含建议产品的报告。权重实际上只是每个问题都有一定数值的产品。
例如:
“你抽烟吗?”
如果回答“是”,则应用 以下:核心(1),Alpha(4), 肝(2),玉(1),Rejuve(4)
我已经设置了初始模型和管理员,但我对如何编写视图感到困惑。他们将能够添加所需答案的问题,然后根据需要添加尽可能多的权重(对产品进行外键处理)和每个问题的值。
以下是我的模特:
from django.db import models
from src.products.models import Product
"""
This app has 'yes' or 'no' questions. Each question has several 'weightings'
atached to it which are tallied at the end to recommend specific products
to the user doing the questionairre.
"""
class Question(models.Model):
"""Yes or no questions to gether enough info to recommend products via the weightings."""
ANSWER_CHOICES = (
('y', 'Yes'),
('n', 'No'),
)
GENDER_CHOICES = (
('a', 'All'),
('m', 'Male'),
('f', 'Female'),
)
question_text = models.CharField(
help_text="The text to be displayed for the question",
max_length=300
)
answer = models.CharField(
help_text="Weightings will be applied based on the answer. <br> Ex: If 'Yes' is chosen for 'Do you smoke?', then weightings will be applied if the user answers 'Yes'",
max_length=1,
choices=ANSWER_CHOICES
)
applies_to = models.CharField(
help_text="Used to filter questions based on gender of user answering the questions.",
max_length=1,
choices=GENDER_CHOICES
)
order = models.IntegerField(
help_text="Used to determine the ordering of the questions. If left blank, the question will appear after those which are ordered.",
blank=True,
null=True,
)
class Meta:
verbose_name_plural = 'questions'
ordering = ['order']
def __unicode__(self):
return self.question_text
class Weighting(models.Model):
"""References products with a specified weighting.
Added up at end to recommend products."""
question = models.ForeignKey(Question)
product = models.ForeignKey(Product)
value = models.IntegerField()
def __unicode__(self):
return '%s %s %s' % (self.question, self.product, self.value)
我现在正在手动创建表单。我认为这是最好的方式。
那么,什么是获得帖子答案的最佳方式,然后将答案与所需答案进行比较,并获得所有问题的权重总和?
总和的最后一部分似乎是最困难的。
思考和建议?
编辑:用于参考:
答案 0 :(得分:2)
您要做的是基本上在Question
和Product
之间建立多对多关系,并通过向关系(值)添加字段来加权此关系;有一些documentation关于如何以django的方式!
我想你需要一个模型来存储用户给出的答案,它可能是这样的:
from django.contrib.auth.models import User
class Answer(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
answer = models.CharField(choices=ANSWER_CHOICES, max_length=1)
在您存储了答案后,我想唯一的方法是迭代一个用户的答案,然后通过答案回答并在问题得到正确答案的情况下总结值!
编辑:这个解决方案让用户的答案在数据库中保持持久性,你必须自己想一想你想用它们做什么,否则你可以把它们存储在一个会话中,例如。在request.session
的字典中。
编辑:以一种形式提出问题尝试类似:
class QuestionForm(forms.Form):
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=':',
empty_permitted=False):
super(QuestionForm, self).__init__(data, files, auto_id, prefix,
initial, error_class, label_suffix, empty_permitted)
self.questions = Question.objects.all()
if self.questions:
for question in self.question:
self.fields['question_%s' % question.pk] =\
forms.ChoiceField(label=question.question_text,
choices=[(q.pk, q.answer) for q in question.answer_set.all()])