如何在 Django 中进行可定制的用户调查

发布于 2024-07-25 02:23:30 字数 995 浏览 12 评论 0原文

我正在为一家公司制作一个系统,其中必须保存有关各种事物的满意度的信息,我已经使用带有固定问题和答案的固定模型使其正常工作,但我确信他们需要更改或添加问题。

因此,我想创建一个系统,用户可以在其中创建由他们定义的自定义问题组成的自定义评估模式。 我该如何进行这样的设计?

现在我的模型是这样的,但是错误的:

RATING_CHOICES = ((0, u"Good"), (1, u"Bad"), (2, u"Dunno"),)

class EvaluationScheme(models.Model):
    title = models.CharField(max_length=200)

class Evaluation(models.Model):
    doctor = models.CharField(max_length=200)
    agency = models.CharField(max_length=200)
    scheme = models.ForeignKey(EvaluationScheme)

class EvaluationQuestion(models.Model):
    question = models.CharField(max_length=200)
    evaluation = models.ForeignKey(EvaluationScheme)

    def __unicode__(self):
        return self.question

class EvaluationAnswer(models.Model):
    evaluation = models.ForeignKey(Evaluation)
    question = models.ForeignKey(EvaluationQuestion)
    answer = models.SmallIntegerField(choices=RATING_CHOICES)

这就是我想要的,除了评估方案是无用的,因为你仍然必须自己选择所有问题和答案 - 它不显示仅与相关的问题的列表选择的模式。

I am making a system for a company which among other things must hold information about the satisfactory level about various things, I have made it work fine using a fixed model with fixed questions and answers, but I am sure that they will need to change or add questions.

So I want to make a system where users can make custom evaluation schemas that consists of custom questions defined by them. How do I go about making such a design?

Right now my model is this, but wrong:

RATING_CHOICES = ((0, u"Good"), (1, u"Bad"), (2, u"Dunno"),)

class EvaluationScheme(models.Model):
    title = models.CharField(max_length=200)

class Evaluation(models.Model):
    doctor = models.CharField(max_length=200)
    agency = models.CharField(max_length=200)
    scheme = models.ForeignKey(EvaluationScheme)

class EvaluationQuestion(models.Model):
    question = models.CharField(max_length=200)
    evaluation = models.ForeignKey(EvaluationScheme)

    def __unicode__(self):
        return self.question

class EvaluationAnswer(models.Model):
    evaluation = models.ForeignKey(Evaluation)
    question = models.ForeignKey(EvaluationQuestion)
    answer = models.SmallIntegerField(choices=RATING_CHOICES)

This is sort of what I want, except that the EvaluationScheme is useless, since you still have to chose all questions and answers yourself - it does not display a list of only the questions related to the schema of choice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

差↓一点笑了 2024-08-01 02:23:30

我认为你的模型很好。 我使用 Django 管理员创建了一个包含评估问题的评估方案,然后我创建了一个评估并且能够回答它的问题。 这是我用来处理你的模型的代码:

# forms.py:
from django.forms.models import inlineformset_factory
import models

AnswerFormSet = inlineformset_factory(models.Evaluation, 
        models.EvaluationAnswer, exclude=('question',), 
        extra=0, can_delete=False)

# views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
import models, forms

def prepare_blank_answers(evaluation):
    for question in evaluation.scheme.evaluationquestion_set.all():
        answer = models.EvaluationAnswer(evaluation=evaluation,
                                         question=question)
        answer.save()

def answer_form(request, id):
    evaluation = get_object_or_404(models.Evaluation, id=id)
    if len(evaluation.evaluationanswer_set.all()) == 0:
        prepare_blank_answers(evaluation)
    if request.method == 'POST':
        formset = forms.AnswerFormSet(request.POST, instance=evaluation)
        if formset.is_valid():
            formset.save()
            return HttpResponse('Thank you!')
    else:
        formset = forms.AnswerFormSet(instance=evaluation)
    return render_to_response('answer_form.html',
            {'formset':formset, 'evaluation':evaluation})


# answer_form.html:
<html><head></head><body>
  Doctor: {{ evaluation.doctor }} <br>
  Agency: {{ evaluation.agency }}
  <form method="POST">
    {{ formset.management_form }}
    <table>
      {% for form in formset.forms %}
        <tr><th colspan="2">{{ form.instance.question }}</th></tr>
        {{ form }}
      {% endfor %}
    </table>
    <input type="submit">
  </form>
</body></html>

I think your models are fine. I used the Django admin to create an EvaluationScheme with EvaluationQuestions, then I created an Evaluation and I was able to answer its questions. Here's the code I used to go with your models:

# forms.py:
from django.forms.models import inlineformset_factory
import models

AnswerFormSet = inlineformset_factory(models.Evaluation, 
        models.EvaluationAnswer, exclude=('question',), 
        extra=0, can_delete=False)

# views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
import models, forms

def prepare_blank_answers(evaluation):
    for question in evaluation.scheme.evaluationquestion_set.all():
        answer = models.EvaluationAnswer(evaluation=evaluation,
                                         question=question)
        answer.save()

def answer_form(request, id):
    evaluation = get_object_or_404(models.Evaluation, id=id)
    if len(evaluation.evaluationanswer_set.all()) == 0:
        prepare_blank_answers(evaluation)
    if request.method == 'POST':
        formset = forms.AnswerFormSet(request.POST, instance=evaluation)
        if formset.is_valid():
            formset.save()
            return HttpResponse('Thank you!')
    else:
        formset = forms.AnswerFormSet(instance=evaluation)
    return render_to_response('answer_form.html',
            {'formset':formset, 'evaluation':evaluation})


# answer_form.html:
<html><head></head><body>
  Doctor: {{ evaluation.doctor }} <br>
  Agency: {{ evaluation.agency }}
  <form method="POST">
    {{ formset.management_form }}
    <table>
      {% for form in formset.forms %}
        <tr><th colspan="2">{{ form.instance.question }}</th></tr>
        {{ form }}
      {% endfor %}
    </table>
    <input type="submit">
  </form>
</body></html>
因为看清所以看轻 2024-08-01 02:23:30

您检查过 django-survey 吗? 非常整洁。

Have you checked django-survey? It's pretty neat.

凉风有信 2024-08-01 02:23:30

Django-crowdaround 是 django-survey 的一个分支,自 2012 年起一直在积极维护,目标是姜戈 1.2+。

Django-crowdsourcing is a fork of django-survey that is actively maintained as of 2012 and targets Django 1.2+.

半寸时光 2024-08-01 02:23:30

不是 django 专家,因此您可能希望等待更有经验的人来回答,但您可以尝试以下操作:

EvaluationQuestions.objects.filter(evaluationscheme__title="myscheme").select_related()

也可以将关系反过来,取决于您需要如何访问数据。

class EvaluationScheme(models.Model):
    title = models.CharField(max_length=200)
    evaluations = models.ManyToMany(Evaluation)
    questions = models.ManyToMany(EvaluationQuestions)

Not a django expert so you might wish to wait for a more experience person to answer but you could try something like:

EvaluationQuestions.objects.filter(evaluationscheme__title="myscheme").select_related()

Could also put the relationships the other way around, depends how you need to access the data.

class EvaluationScheme(models.Model):
    title = models.CharField(max_length=200)
    evaluations = models.ManyToMany(Evaluation)
    questions = models.ManyToMany(EvaluationQuestions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文