0

我有一个模型如下:

class Venture(models.Model):
    name = models.CharField(_('name'), max_length=255)
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField()


class QuestionSet(models.Model):
    title = models.CharField(_(u'title'), max_length=100)


class Question(models.Model):
    title = models.CharField(_(u'title'), max_length=255)
    qset = models.ForeignKey(QuestionSet, related_name='questions')


class Answer(models.Model):
    question = models.ForeignKey(Question, related_name='answers')
    responder = models.ForeignKey(User)
    venture = models.ForeignKey(Venture, related_name='answers')
    text = models.TextField(_(u'answer'), blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

存在一组针对所有用户的预定义问题。对于每个 Venture,我都有一个针对每个 QuestionSet 的页面,其中列出了该集合中的问题,然后我按如下方式遍历问题:

<div> {{ venture.name }} </div>
{% for question in qset.questions.all %}
  <div class="qset-question control-group">
    {{ question.title }}
    {# How do I access the answer for the current venture? #}
  </div>
{% endfor %}

问题是为当前的企业获得该问题的答案的最佳方式是什么。我想在这里输出一些关于答案的信息。

任何帮助表示赞赏。

4

3 回答 3

7

我通过创建自定义模板标签解决了这个问题。这是代码:

@register.assignment_tag
def question_answer(venture, question):
    answers = question.answers.filter(venture=venture)
    return answers[0] if answers else None

然后像这样使用它:

{% question_answer venture question as answer %}
{{ answer }}
于 2013-05-20T12:55:37.460 回答
0

最简单的解决方案是为给定的“风险”选择答案,这将为您提供答案列表。然后,您可以使用它来获取企业的所有问题。

所以是这样的:

ans = Answer.objects.filter(venture=v)

编辑::

根据您所说的,您需要重新设计模型。我可以看到一些缺点(我会让你弄清楚)。否则,您需要执行多个查询并为您的视图提供多个查询集。

于 2013-05-20T11:31:30.967 回答
0

这样的事情呢?

模型.py

from django.db import models
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext as _

User = get_user_model()

class Venture(models.Model):
    name = models.CharField(_('name'), max_length=255)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

class Question(models.Model):
    title = models.CharField(_(u'title'), max_length=255)

    def __str__(self):
        return self.title

class VentureQuestion(models.Model):
    venture = models.ForeignKey('Venture', related_name='questions')
    question = models.ForeignKey('Question', related_name='venture_questions')

    def __str__(self):
        return "{}: {}".format(self.venture, self.question)

class Answer(models.Model):
    question = models.ForeignKey('VentureQuestion', related_name='answers')
    responder = models.ForeignKey(User, related_name='answers')
    text = models.TextField(_(u'answer'), blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{}: {}".format(self.responder.username, self.text)

管理员.py

from django.contrib import admin

from example.models import Venture, VentureQuestion, Question, Answer


class AnswerInline(admin.StackedInline):
    model = Answer
    extra = 0


class VentureQuestionAdmin(admin.ModelAdmin):
    inlines = [AnswerInline]


admin.site.register(Venture)
admin.site.register(VentureQuestion, VentureQuestionAdmin)
admin.site.register(Question)
admin.site.register(Answer)

这样,您就可以获得与...Question相关的每个问题的答案Venture(听起来这至少是您正在寻找的功能...)

>>> from example.models import Venture
>>> ventures = Venture.objects.all()
>>> for venture in ventures:
...     for venture_question in venture.questions.all():
...         venture_question.question.title
...
u'What is this?'
u'How does this work?'
u'Does this even work?'
>>> for venture in ventures:
...     for venture_question in venture.questions.all():
...         venture_question.question.title, [answer.text for answer in venture_question.answers.all()]
...         
(u'What is this?', [])
(u'How does this work?', [u'It just does!'])
(u'Does this even work?', [u'Sure it does...', u'I think so'])

你可以有另一个Venture使用相同Question但有不同的Answers

>>> venture = Venture.objects.get(name='Another Venture')
>>> for venture_question in venture.questions.all():
...     venture_question.question.title, [answer.text for answer in venture_question.answers.all()]
...     
(u'What is this?', [])

添加Answer到新的之后VentureQuestion

>>> venture = Venture.objects.get(name='Another Venture')
>>> for venture_question in venture.questions.all():
...     venture_question.question.title, [answer.text for answer in venture_question.answers.all()]
...     
(u'What is this?', [u"It's another venture with the same question and it's own answers..."])
于 2013-05-20T15:31:35.213 回答