class Comment(models.Model):
parent_comment=models.ForeignKey(
to='self',
related_name='_comments',
on_delete=models.DO_NOTHING,
null=True,
blank=True,
如何预取_comments:child_comment 深度越深,N+1查询问题就越深
我找不到任何做预取的东西,所以我决定最终改变逻辑。我收到了第一条评论,并发表了一条提到它的评论
如果您还有其他好的内容,请告诉我!
parent_comment = models.ForeignKey(
to='self',
related_name='replies',
on_delete=models.SET_NULL,
null=True,
blank=True,
)
replied_comment = models.ForeignKey(
to='self',
related_name='mentions',
on_delete=models.SET_NULL,
null=True,
blank=True,
)