如何在1个视图中逐个使用2个ForeignKey过滤器 - Django?

时间:2016-01-16 00:46:41

标签: python django

您好,感谢您的回答:

My Taks:显示文章,显示3个问题(与本文相关),显示每个问题的3个答案(与这些问题相关)。就像一个coomon测试页面。

我的模特:

class Step(models.Model):  #Main article
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    annotation = models.TextField()
    main_text = models.TextField()   

    def __str__(self):
        return self.title

class Question(models.Model): #Questios related to Article.
    step = models.ForeignKey(Step, on_delete=models.CASCADE)
    title = models.CharField(max_length=200, default = "pages")
    question_text = models.TextField()
    question_name = models.CharField(max_length=40, help_text="английские буквы", blank=True, null=True)

class Answer(models.Model): #Answers related to Questions
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.TextField()
    votes = models.IntegerField(default=0)
    answer_name = models.CharField(max_length=40, help_text="английские буквы", blank=True, null=True)

如何运作

我在url中使用identifyicator,向django显示它应该显示的文章。它也帮助我筛选本文的问题。然后我应该为每个问题提交答案,我以前有过。有很多文章,问题和答案,所以你不能使用对象()。所有。

url(r'^question(?P<question_id>[0-9]+)$', 'bakot.views.question', name='question'), #urls.py

def question(request, question_id):
    stepfields = get_object_or_404(Step, id = question_id) #get an article
    questionship = Question.objects.filter(step_id = question_id) #get questions, related to this article.
    answership = questionship.prefetch_related().all #this is doesn't work. And i couldn't use coommon filter, as i don't know id for them. Or i could set ID and it will show the same answers for 3 questions.        
    context = {
        "stepfieldst" : stepfields,
        "questionship" : questionship,
        "answership" : answership,
    }
    return render(request, 'bakot/question.html', context)

我如何在模板中显示:(有问题和答案的部分)

{% block question_area %}
  {% for question in questionship %}
  <div class="small-12 medium-12 large-12 test-remark">
    <legend><strong>{{ question.title }}&nbsp;</strong>{{ question.question_text }}</legend>

      <ul class="menu vertical">

        {% for answer in answership  %}
        <li>
          <ul class="menu test-answer-padding navigation_hover_link11">
            <li class="test-dot-padding"><input type="radio" name="{{ question.question_name }}" value="{{ answer.answer_name }}" id="pokemonRed" required></li>
            <li><label for="pokemonRed">{{ answer.choice_text }}</label>
          </ul>
        </li>

        {% endfor %}

      </ul>

  </div>
  {% endfor %}
{% endblock %}

1 个答案:

答案 0 :(得分:1)

我会在这里做一些事情来为您提供最佳解决方案。

首先,您不需要在上下文中包含<TextBox ToolTipService.ShowDuration="120000" ToolTip="{Binding ThreatText}" Name="txtThreat" Text="{Binding ThreatText}" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Margin="3" Grid.Row="8" Grid.Column="1" Grid.ColumnSpan="3" Grid.RowSpan="1" IsReadOnly="True" Height="30"/> txtThreat.Background = new SolidColorBrush(Colors.Yellow); 。您的模板可以通过questionshipanswership获取。

接下来,为了将列表限制为3,我将实现模板过滤器。这可以使用切片来完成,如下所示:

stepfields.questions

然后,您可以在模板中将question.answers循环更改为以下格式:

register = template.Library()

@register.filter
def limit_to_3(list, arg):
    return list[0:arg]

根据每篇文章的问题和答案数量是否足够低以至于查询所有问题和答案都会比多次查询更轻松,您可以执行以下操作:

{% for %}

这将在单个查询中选择所有相关问题和答案。