django 加权问卷?
我学习Django已经有几年了,算是个进阶的初学者,哈哈。我正在为一个客户做一个“加权问卷”(这是我能想到的最好名字),但在这个项目上我有点迷茫,不知道该怎么继续。
客户想出了几个是非题,根据答案的不同,会有不同的权重。最后在提交后,我需要检查答案,计算权重的总和,然后根据这些权重显示一个推荐产品的报告。这些权重其实就是每个问题对应的一些产品和它们的数值。
举个例子:
“你抽烟吗?”
如果回答“是”,那么就应用以下权重:Core(1), Alpha(4), Liver(2), Jade(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)
现在我正在手动创建表单。我觉得这样做是最好的方式。
那么,获取答案后,怎么比较答案和期望的答案,并计算所有问题的权重总和呢?
最后那部分计算总和似乎是最难的。
大家有什么想法和建议吗?
编辑:作为参考:
1 个回答
2
你想做的其实就是在 Question
(问题)和 Product
(产品)之间建立一种多对多的关系,并通过添加一个字段(值)来给这个关系加权。关于如何在 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()])