如何在反向外键上选择相关?
A BlogPost
有很多评论
。我想获取 BlogPost
及其所有评论的列表。
因此,我有
BlogPost.objects.filter(my_filter).select_related()
但外键位于 Comment
上,而不是 BlogPost
上,因此 select_lated()
不会预取任何评论。有办法让它发挥作用吗?
我无法反转查询 (Comment.objects...
),因为 select_lated()
确实 获取的其他对象将不会'不工作。我需要它双向工作。
Possible Duplicate:
A left outer reverse select_related in Django?
A BlogPost
has many Comment
s. I want to get a list of BlogPost
s and all their comments.
Thus, I have
BlogPost.objects.filter(my_filter).select_related()
But the ForeignKey is on the Comment
, not the BlogPost
, so the select_related()
doesn't prefetch any comments. Is there a way to get this to work?
I can't reverse the query (Comment.objects...
) because then the other objects that the select_related()
does fetch wouldn't work. I need it to work both ways.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不获取评论,然后使用 重新分组 模板标签来显示它们:
然后在模板中:
Why won't you fetch the comments and then use regroup template tag to display them:
Then in template: