django视图

请问一个关于视图的问题

有3个模型。blog,Comment(foreignkey 是blog),SubComment(foreignkey 是Comment),如何通过blog.id, 视图显示所有的Comment和SubComment呢?

评论 2

  • comment_list = list() subcomment_list = list() blog_objs = blog.objects.filter(id=id) for blog_obj in blog_objs: comment_objs = Comment.objects.filter(forignkey_name=blog_obj) comment_list.extend(list(comment_objs)) for comment_obj in comment_objs: subcomment_objs = SubComment.objects.filter(forignkey_name=comment_obj) subcomment_list.extend(list(subcomment_objs))

    comment_list和subcomment_list就是你想要的结果了