From dca67bb2c27cfa1b23cb6519c544ce467813e0ec Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Thu, 24 Aug 2017 01:26:24 +0500 Subject: [PATCH] Refs #28459 -- Improved performance of SQLCompiler.apply_converters(). --- django/db/models/query.py | 5 ++--- django/db/models/sql/compiler.py | 9 +++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 2a53fa3e89..512ebad784 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -119,7 +119,6 @@ class ValuesListIterable(BaseIterable): query = queryset.query compiler = query.get_compiler(queryset.db) - results = compiler.results_iter() if queryset._fields: field_names = list(query.values_select) extra_names = list(query.extra_select) @@ -133,8 +132,8 @@ class ValuesListIterable(BaseIterable): # Reorder according to fields. index_map = {name: idx for idx, name in enumerate(names)} rowfactory = operator.itemgetter(*[index_map[f] for f in fields]) - results = map(rowfactory, results) - return results + return map(rowfactory, compiler.results_iter()) + return compiler.results_iter(tuple_expected=True) class FlatValuesListIterable(BaseIterable): diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index f92df8c996..2afc3635fa 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -942,16 +942,15 @@ class SQLCompiler: def apply_converters(self, rows, converters): connection = self.connection converters = list(converters.items()) - for row in rows: - row = list(row) + for row in map(list, rows): for pos, (convs, expression) in converters: value = row[pos] for converter in convs: value = converter(value, expression, connection) row[pos] = value - yield tuple(row) + yield row - def results_iter(self, results=None): + def results_iter(self, results=None, tuple_expected=False): """Return an iterator over the results from executing this query.""" if results is None: results = self.execute_sql(MULTI) @@ -960,6 +959,8 @@ class SQLCompiler: rows = chain.from_iterable(results) if converters: rows = self.apply_converters(rows, converters) + if tuple_expected: + rows = map(tuple, rows) return rows def has_results(self):