gpt4 book ai didi

python - Django预取相关和选择相关

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:34 24 4
gpt4 key购买 nike

我想知道 prefetch_lated() 和 select_lated() 的行为。如果我执行类似 Model.objects.filter(...).prefetch_lated() 的操作,我会注意到发生的数据库查询要少得多。所以我最初的猜测是,如果没有在 prefetch_lated() 中指定所需的查找,它将自动遍历所有模型字段并完成所需的工作。然而,我在网上找不到任何关于它的引用,这对我来说似乎很奇怪。我的猜测是正确的还是我遗漏了什么?

最佳答案

来自the FineManual(tm) (重点是我的):

There may be some situations where you wish to call select_related() with a lot of related objects, or where you don’t know all of the relations. In these cases it is possible to call select_related() with no arguments. This will follow all non-null foreign keys it can find - nullable foreign keys must be specified. This is not recommended in most cases as it is likely to make the underlying query more complex, and return more data, than is actually needed.

关于python - Django预取相关和选择相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47178340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com