From b7e5b5ba1ed03243f53f9f0bbaa58a33691c815d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Mon, 23 Sep 2013 18:18:20 +0300 Subject: [PATCH] [1.5.x] Fixed #21126 -- QuerySet value conversion failure A .annotate().select_related() query resulted in misaligned rows vs columns for compiler.resolve_columns() method. Report & patch by Michael Manfre. Backpatch of 83554b018ef283827c0e7459ab934d447b3419d5 from master. --- django/db/backends/__init__.py | 2 +- django/db/models/sql/compiler.py | 10 ++++++++-- tests/regressiontests/aggregation_regress/tests.py | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 1decce02163..d33744c9605 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -895,7 +895,7 @@ class BaseDatabaseOperations(object): Coerce the value returned by the database backend into a consistent type that is compatible with the field type. """ - if value is None: + if value is None or field is None: return value internal_type = field.get_internal_type() if internal_type == 'FloatField': diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 7ea4cd700af..18741655d86 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -774,6 +774,9 @@ class SQLCompiler(object): transaction.set_dirty(self.using) for rows in self.execute_sql(MULTI): for row in rows: + if has_aggregate_select: + aggregate_start = len(self.query.extra_select) + len(self.query.select) + aggregate_end = aggregate_start + len(self.query.aggregate_select) if resolve_columns: if fields is None: # We only set this up here because @@ -800,11 +803,14 @@ class SQLCompiler(object): db_table = self.query.model._meta.db_table fields = [f for f in fields if db_table in only_load and f.column in only_load[db_table]] + if has_aggregate_select: + # pad None in to fields for aggregates + fields = fields[:aggregate_start] + [ + None for x in range(0, aggregate_end - aggregate_start) + ] + fields[aggregate_start:] row = self.resolve_columns(row, fields) if has_aggregate_select: - aggregate_start = len(self.query.extra_select) + len(self.query.select) - aggregate_end = aggregate_start + len(self.query.aggregate_select) row = tuple(row[:aggregate_start]) + tuple([ self.query.resolve_aggregate(value, aggregate, self.connection) for (alias, aggregate), value diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py index 71c90ecb5ef..65d345af52b 100644 --- a/tests/regressiontests/aggregation_regress/tests.py +++ b/tests/regressiontests/aggregation_regress/tests.py @@ -383,6 +383,17 @@ class AggregationTests(TestCase): qs = Entries.objects.annotate(clue_count=Count('clues__ID')) self.assertQuerysetEqual(qs, []) + def test_boolean_conversion(self): + # Aggregates mixed up ordering of columns for backend's convert_values + # method. Refs #21126. + e = Entries.objects.create(Entry='foo') + c = Clues.objects.create(EntryID=e, Clue='bar') + qs = Clues.objects.select_related('EntryID').annotate(Count('ID')) + self.assertQuerysetEqual( + qs, [c], lambda x: x) + self.assertEqual(qs[0].EntryID, e) + self.assertIs(qs[0].EntryID.Exclude, False) + def test_empty(self): # Regression for #10089: Check handling of empty result sets with # aggregates