From 4dfd6b88d520b43b6363946e5ee58ba14cd1efe6 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 22 Aug 2017 20:26:07 +0500 Subject: [PATCH] Refs #28459 -- Improved performance of FlatValuesListIterable. --- django/db/models/query.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 0f55268b18..2a53fa3e89 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -8,6 +8,7 @@ import sys import warnings from collections import OrderedDict, deque from contextlib import suppress +from itertools import chain from django.conf import settings from django.core import exceptions @@ -145,8 +146,7 @@ class FlatValuesListIterable(BaseIterable): def __iter__(self): queryset = self.queryset compiler = queryset.query.get_compiler(queryset.db) - for row in compiler.results_iter(): - yield row[0] + return chain.from_iterable(compiler.results_iter()) class QuerySet: @@ -302,6 +302,9 @@ class QuerySet: # METHODS THAT DO DATABASE QUERIES # #################################### + def _iterator(self, use_chunked_fetch, chunk_size): + yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size) + def iterator(self, chunk_size=2000): """ An iterator over the results from applying this QuerySet to the @@ -310,7 +313,7 @@ class QuerySet: if chunk_size <= 0: raise ValueError('Chunk size must be strictly positive.') use_chunked_fetch = not connections[self.db].settings_dict.get('DISABLE_SERVER_SIDE_CURSORS') - return iter(self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)) + return self._iterator(use_chunked_fetch, chunk_size) def aggregate(self, *args, **kwargs): """