首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django加权问卷?

django加权问卷?
EN

Stack Overflow用户
提问于 2010-08-29 06:20:26
回答 1查看 643关注 0票数 2

我学习django已经有几年了,我认为自己是一个高级初学者,哈。我正在为一个客户编写一份“加权问卷”(这是我能给出的最好的名字),并且一直在考虑如何使用它。

客户已经提出了一系列的“是”或“否”的问题,根据答案的不同,将应用某些权重。在提交的最后,我需要检查答案并将权重加起来,而不是根据权重显示一份带有建议产品的报告。这些权重实际上只是对每个问题都有一定数值的产品。

例如:

“你抽烟吗?” 如果回答为“是”,则应用以下方法:核心(1)、α(4)、肝脏(2)、Jade(1)、Rejuve(4)

我已经设置了最初的模型和管理,但有点困惑如何编写视图。他们将能够添加问题与期望的答案,然后是多少权重(这是外键产品)和价值,每个问题,他们想要。

以下是我的模特:

代码语言:javascript
复制
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)

我现在正在手工创建表单。我觉得这是最好的办法。

,那么,在帖子中获得答案的最佳方法是什么,然后将答案与所需的答案进行比较,并得到所有问题的权重之和?

最后一部分加起来似乎是最困难的。

想法和建议?

编辑:参考使用:

http://questionnaire.corporatism.org/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-29 19:21:15

您要做的基本上是在QuestionProduct之间建立一个多到多的关系,并通过在关系(值)中添加一个字段来加权这个关系;关于这个问题,有一些文档是如何以django的方式实现的!我想你还需要一个模型来存储用户给出的答案,可能是这样的:

代码语言:javascript
复制
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的字典里。

编辑:要以一种形式获得问题,请尝试如下所示:

代码语言:javascript
复制
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()])
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3593756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档