_set.all列表中的每个项目

时间:2015-10-21 06:11:20

标签: django foreign-key-relationship

我有一个django应用程序,其中有特定答案的评论。现在我想访问每个答案的评论并将其呈现给模板。

models.py

class Comment(models.Model):
    comment_text = models.TextField()
    user_id = models.ForeignKey(MyUser,blank=True)
    answer_id = models.ForeignKey(Answer, blank=True)
    comment_timestamp=models.DateTimeField(default=datetime.now())
    question_id = models.ForeignKey(Question, blank=True,null=True)
    anonymous_user=models.CharField(max_length=20,null=True)
在views.py中

answer = question.answer_set.all().order_by('-answer_timestamp')

给我一​​个答案清单

[<Answer: It is a long established fact that a reader will be distracted by the .>, <Answer:It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.>]

现在我想访问每个答案的评论。 回答[0] .set_all,回答[1] .set_all是不可能的,因为可以有更多答案。

我试过了

{% for comment in ans.comment_set.all %}

{% endfor %}
在django模板中

但它没有给出所需的结果

任何其他方法,以便可以在views.py

中的上下文中传递注释

0 个答案:

没有答案