如何在 django 中预取评论

问题描述 投票:0回答:1
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查询问题就越深

django prefetch
1个回答
0
投票

我找不到任何做预取的东西,所以我决定最终改变逻辑。我收到了第一条评论,并发表了一条提到它的评论

如果您还有其他好的内容,请告诉我!

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,
)
© www.soinside.com 2019 - 2024. All rights reserved.