django视图 请问一个关于视图的问题 有3个模型。blog,Comment(foreignkey 是blog),SubComment(foreignkey 是Comment),如何通过blog.id, 视图显示所有的Comment和SubComment呢? 评论 2 登录评论 吴秀峰 2019-09-02 递归了解一下! 一介书生 2019-10-04 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就是你想要的结果了
一介书生 2019-10-04 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就是你想要的结果了
评论 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就是你想要的结果了