mirror of https://github.com/django/django.git
[1.6.x] Fixed #21413 -- resolve_columns fields misalignment
Backpatch of 9918c11114
from master.
Conflicts:
django/db/models/sql/compiler.py
tests/model_inheritance_regress/tests.py
This commit is contained in:
parent
5f42c02195
commit
0f272629ca
|
@ -132,7 +132,7 @@ class GeoSQLCompiler(compiler.SQLCompiler):
|
||||||
if table in only_load and field.column not in only_load[table]:
|
if table in only_load and field.column not in only_load[table]:
|
||||||
continue
|
continue
|
||||||
if as_pairs:
|
if as_pairs:
|
||||||
result.append((alias, field.column))
|
result.append((alias, field))
|
||||||
aliases.add(alias)
|
aliases.add(alias)
|
||||||
continue
|
continue
|
||||||
# This part of the function is customized for GeoQuery. We
|
# This part of the function is customized for GeoQuery. We
|
||||||
|
|
|
@ -289,7 +289,7 @@ class SQLCompiler(object):
|
||||||
if table in only_load and field.column not in only_load[table]:
|
if table in only_load and field.column not in only_load[table]:
|
||||||
continue
|
continue
|
||||||
if as_pairs:
|
if as_pairs:
|
||||||
result.append((alias, field.column))
|
result.append((alias, field))
|
||||||
aliases.add(alias)
|
aliases.add(alias)
|
||||||
continue
|
continue
|
||||||
if with_aliases and field.column in col_aliases:
|
if with_aliases and field.column in col_aliases:
|
||||||
|
@ -650,10 +650,10 @@ class SQLCompiler(object):
|
||||||
_, _, _, joins, _ = self.query.setup_joins(
|
_, _, _, joins, _ = self.query.setup_joins(
|
||||||
[f.name], opts, root_alias, outer_if_first=promote)
|
[f.name], opts, root_alias, outer_if_first=promote)
|
||||||
alias = joins[-1]
|
alias = joins[-1]
|
||||||
columns, aliases = self.get_default_columns(start_alias=alias,
|
columns, _ = self.get_default_columns(start_alias=alias,
|
||||||
opts=f.rel.to._meta, as_pairs=True)
|
opts=f.rel.to._meta, as_pairs=True)
|
||||||
self.query.related_select_cols.extend(
|
self.query.related_select_cols.extend(
|
||||||
SelectInfo(col, field) for col, field in zip(columns, f.rel.to._meta.concrete_fields))
|
SelectInfo((col[0], col[1].column), col[1]) for col in columns)
|
||||||
if restricted:
|
if restricted:
|
||||||
next = requested.get(f.name, {})
|
next = requested.get(f.name, {})
|
||||||
else:
|
else:
|
||||||
|
@ -678,11 +678,10 @@ class SQLCompiler(object):
|
||||||
alias = joins[-1]
|
alias = joins[-1]
|
||||||
from_parent = (opts.model if issubclass(model, opts.model)
|
from_parent = (opts.model if issubclass(model, opts.model)
|
||||||
else None)
|
else None)
|
||||||
columns, aliases = self.get_default_columns(start_alias=alias,
|
columns, _ = self.get_default_columns(start_alias=alias,
|
||||||
opts=model._meta, as_pairs=True, from_parent=from_parent)
|
opts=model._meta, as_pairs=True, from_parent=from_parent)
|
||||||
self.query.related_select_cols.extend(
|
self.query.related_select_cols.extend(
|
||||||
SelectInfo(col, field) for col, field
|
SelectInfo((col[0], col[1].column), col[1]) for col in columns)
|
||||||
in zip(columns, model._meta.concrete_fields))
|
|
||||||
next = requested.get(f.related_query_name(), {})
|
next = requested.get(f.related_query_name(), {})
|
||||||
# Use True here because we are looking at the _reverse_ side of
|
# Use True here because we are looking at the _reverse_ side of
|
||||||
# the relation, which is always nullable.
|
# the relation, which is always nullable.
|
||||||
|
@ -728,8 +727,10 @@ class SQLCompiler(object):
|
||||||
# found in get_columns(). It would be nice to clean this up.
|
# found in get_columns(). It would be nice to clean this up.
|
||||||
if self.query.select:
|
if self.query.select:
|
||||||
fields = [f.field for f in self.query.select]
|
fields = [f.field for f in self.query.select]
|
||||||
else:
|
elif self.query.default_cols:
|
||||||
fields = self.query.get_meta().concrete_fields
|
fields = self.query.get_meta().concrete_fields
|
||||||
|
else:
|
||||||
|
fields = []
|
||||||
fields = fields + [f.field for f in self.query.related_select_cols]
|
fields = fields + [f.field for f in self.query.related_select_cols]
|
||||||
|
|
||||||
# If the field was deferred, exclude it from being passed
|
# If the field was deferred, exclude it from being passed
|
||||||
|
|
|
@ -441,3 +441,9 @@ class ModelInheritanceTest(TestCase):
|
||||||
# used in the qs and top contains direct pointer to the bottom model.
|
# used in the qs and top contains direct pointer to the bottom model.
|
||||||
qs = ItalianRestaurant.objects.values_list('serves_gnocchi').filter(name='foo')
|
qs = ItalianRestaurant.objects.values_list('serves_gnocchi').filter(name='foo')
|
||||||
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
||||||
|
|
||||||
|
def test_inheritance_resolve_columns(self):
|
||||||
|
Restaurant.objects.create(name='Bobs Cafe', address="Somewhere",
|
||||||
|
serves_pizza=True, serves_hot_dogs=True)
|
||||||
|
p = Place.objects.all().select_related('restaurant')[0]
|
||||||
|
self.assertIsInstance(p.restaurant.serves_pizza, bool)
|
||||||
|
|
Loading…
Reference in New Issue