这是从
django docs on the queryset iterator() method:
A QuerySet typically caches its results internally so that repeated evaluations
do not result in additional queries. In contrast, iterator() will read results
directly, without doing any caching at the QuerySet level (internally, the defaul
t iterator calls iterator() and caches the return value). For a QuerySet which
returns a large number of objects that you only need to access once,
this can results in better performance and a significant reduction in memory.
阅读后,我仍然感到困惑:关于增加性能和内存减少的线条表明我们应该使用iterator()方法.有人可以给出一些好坏的例子iterator()的用法吗?
即使查询结果未被缓存,如果他们真的想要多次访问模型,那么有人只能执行以下操作呢?
saved_queries = list(Model.objects.all().iterator())