diff --git a/django/db/models/query.py b/django/db/models/query.py index c625da4bdc..0b65bb56e8 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1227,9 +1227,9 @@ class RawQuerySet: converters = compiler.get_converters([ f.get_col(f.model._meta.db_table) if f else None for f in fields ]) + if converters: + query = compiler.apply_converters(query, converters) for values in query: - if converters: - values = compiler.apply_converters(values, converters) # Associate fields to values model_init_values = [values[pos] for pos in model_init_pos] instance = model_cls.from_db(db, model_init_names, model_init_values) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 5ae5213b14..d6caa34328 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -938,14 +938,17 @@ class SQLCompiler: converters[i] = (convs, expression) return converters - def apply_converters(self, row, converters): - row = list(row) - for pos, (convs, expression) in converters.items(): - value = row[pos] - for converter in convs: - value = converter(value, expression, self.connection) - row[pos] = value - return tuple(row) + def apply_converters(self, rows, converters): + connection = self.connection + converters = list(converters.items()) + for row in rows: + row = list(row) + for pos, (convs, expression) in converters: + value = row[pos] + for converter in convs: + value = converter(value, expression, connection) + row[pos] = value + yield tuple(row) def results_iter(self, results=None): """Return an iterator over the results from executing this query.""" @@ -953,11 +956,10 @@ class SQLCompiler: results = self.execute_sql(MULTI) fields = [s[0] for s in self.select[0:self.col_count]] converters = self.get_converters(fields) - for rows in results: - for row in rows: - if converters: - row = self.apply_converters(row, converters) - yield row + rows = chain.from_iterable(results) + if converters: + rows = self.apply_converters(rows, converters) + return rows def has_results(self): """ diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index bf21c66f20..1eb8598690 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -464,7 +464,7 @@ class Query: result = [None for q in outer_query.annotation_select.items()] converters = compiler.get_converters(outer_query.annotation_select.values()) - result = compiler.apply_converters(result, converters) + result = next(compiler.apply_converters((result,), converters)) return { alias: val