mirror of https://github.com/django/django.git
Merge pull request #1069 from mfogel/ticket_20413
Fixed #20413 - Respect Query.get_meta()
This commit is contained in:
commit
c0c3b65de9
|
@ -121,7 +121,7 @@ class GeoSQLCompiler(compiler.SQLCompiler):
|
||||||
"""
|
"""
|
||||||
result = []
|
result = []
|
||||||
if opts is None:
|
if opts is None:
|
||||||
opts = self.query.model._meta
|
opts = self.get_meta()
|
||||||
aliases = set()
|
aliases = set()
|
||||||
only_load = self.deferred_to_columns()
|
only_load = self.deferred_to_columns()
|
||||||
seen = self.query.included_inherited_models.copy()
|
seen = self.query.included_inherited_models.copy()
|
||||||
|
@ -247,7 +247,7 @@ class GeoSQLCompiler(compiler.SQLCompiler):
|
||||||
used. If `column` is specified, it will be used instead of the value
|
used. If `column` is specified, it will be used instead of the value
|
||||||
in `field.column`.
|
in `field.column`.
|
||||||
"""
|
"""
|
||||||
if table_alias is None: table_alias = self.query.model._meta.db_table
|
if table_alias is None: table_alias = self.get_meta().db_table
|
||||||
return "%s.%s" % (self.quote_name_unless_alias(table_alias),
|
return "%s.%s" % (self.quote_name_unless_alias(table_alias),
|
||||||
self.connection.ops.quote_name(column or field.column))
|
self.connection.ops.quote_name(column or field.column))
|
||||||
|
|
||||||
|
|
|
@ -869,7 +869,7 @@ class QuerySet(object):
|
||||||
"""
|
"""
|
||||||
if self.query.extra_order_by or self.query.order_by:
|
if self.query.extra_order_by or self.query.order_by:
|
||||||
return True
|
return True
|
||||||
elif self.query.default_ordering and self.query.model._meta.ordering:
|
elif self.query.default_ordering and self.query.get_meta().ordering:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -32,7 +32,7 @@ class SQLCompiler(object):
|
||||||
# cleaned. We are not using a clone() of the query here.
|
# cleaned. We are not using a clone() of the query here.
|
||||||
"""
|
"""
|
||||||
if not self.query.tables:
|
if not self.query.tables:
|
||||||
self.query.join((None, self.query.model._meta.db_table, None))
|
self.query.join((None, self.query.get_meta().db_table, None))
|
||||||
if (not self.query.select and self.query.default_cols and not
|
if (not self.query.select and self.query.default_cols and not
|
||||||
self.query.included_inherited_models):
|
self.query.included_inherited_models):
|
||||||
self.query.setup_inherited_models()
|
self.query.setup_inherited_models()
|
||||||
|
@ -260,7 +260,7 @@ class SQLCompiler(object):
|
||||||
"""
|
"""
|
||||||
result = []
|
result = []
|
||||||
if opts is None:
|
if opts is None:
|
||||||
opts = self.query.model._meta
|
opts = self.query.get_meta()
|
||||||
qn = self.quote_name_unless_alias
|
qn = self.quote_name_unless_alias
|
||||||
qn2 = self.connection.ops.quote_name
|
qn2 = self.connection.ops.quote_name
|
||||||
aliases = set()
|
aliases = set()
|
||||||
|
@ -309,7 +309,7 @@ class SQLCompiler(object):
|
||||||
qn = self.quote_name_unless_alias
|
qn = self.quote_name_unless_alias
|
||||||
qn2 = self.connection.ops.quote_name
|
qn2 = self.connection.ops.quote_name
|
||||||
result = []
|
result = []
|
||||||
opts = self.query.model._meta
|
opts = self.query.get_meta()
|
||||||
|
|
||||||
for name in self.query.distinct_fields:
|
for name in self.query.distinct_fields:
|
||||||
parts = name.split(LOOKUP_SEP)
|
parts = name.split(LOOKUP_SEP)
|
||||||
|
@ -338,7 +338,7 @@ class SQLCompiler(object):
|
||||||
ordering = self.query.order_by
|
ordering = self.query.order_by
|
||||||
else:
|
else:
|
||||||
ordering = (self.query.order_by
|
ordering = (self.query.order_by
|
||||||
or self.query.model._meta.ordering
|
or self.query.get_meta().ordering
|
||||||
or [])
|
or [])
|
||||||
qn = self.quote_name_unless_alias
|
qn = self.quote_name_unless_alias
|
||||||
qn2 = self.connection.ops.quote_name
|
qn2 = self.connection.ops.quote_name
|
||||||
|
@ -388,7 +388,7 @@ class SQLCompiler(object):
|
||||||
# 'col' is of the form 'field' or 'field1__field2' or
|
# 'col' is of the form 'field' or 'field1__field2' or
|
||||||
# '-field1__field2__field', etc.
|
# '-field1__field2__field', etc.
|
||||||
for table, cols, order in self.find_ordering_name(field,
|
for table, cols, order in self.find_ordering_name(field,
|
||||||
self.query.model._meta, default_order=asc):
|
self.query.get_meta(), default_order=asc):
|
||||||
for col in cols:
|
for col in cols:
|
||||||
if (table, col) not in processed_pairs:
|
if (table, col) not in processed_pairs:
|
||||||
elt = '%s.%s' % (qn(table), qn2(col))
|
elt = '%s.%s' % (qn(table), qn2(col))
|
||||||
|
@ -556,10 +556,10 @@ class SQLCompiler(object):
|
||||||
select_cols = self.query.select + self.query.related_select_cols
|
select_cols = self.query.select + self.query.related_select_cols
|
||||||
# Just the column, not the fields.
|
# Just the column, not the fields.
|
||||||
select_cols = [s[0] for s in select_cols]
|
select_cols = [s[0] for s in select_cols]
|
||||||
if (len(self.query.model._meta.concrete_fields) == len(self.query.select)
|
if (len(self.query.get_meta().concrete_fields) == len(self.query.select)
|
||||||
and self.connection.features.allows_group_by_pk):
|
and self.connection.features.allows_group_by_pk):
|
||||||
self.query.group_by = [
|
self.query.group_by = [
|
||||||
(self.query.model._meta.db_table, self.query.model._meta.pk.column)
|
(self.query.get_meta().db_table, self.query.get_meta().pk.column)
|
||||||
]
|
]
|
||||||
select_cols = []
|
select_cols = []
|
||||||
seen = set()
|
seen = set()
|
||||||
|
@ -716,14 +716,14 @@ class SQLCompiler(object):
|
||||||
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:
|
else:
|
||||||
fields = self.query.model._meta.concrete_fields
|
fields = self.query.get_meta().concrete_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
|
||||||
# into `resolve_columns` because it wasn't selected.
|
# into `resolve_columns` because it wasn't selected.
|
||||||
only_load = self.deferred_to_columns()
|
only_load = self.deferred_to_columns()
|
||||||
if only_load:
|
if only_load:
|
||||||
db_table = self.query.model._meta.db_table
|
db_table = self.query.get_meta().db_table
|
||||||
fields = [f for f in fields if db_table in only_load and
|
fields = [f for f in fields if db_table in only_load and
|
||||||
f.column in only_load[db_table]]
|
f.column in only_load[db_table]]
|
||||||
row = self.resolve_columns(row, fields)
|
row = self.resolve_columns(row, fields)
|
||||||
|
@ -825,7 +825,7 @@ class SQLInsertCompiler(SQLCompiler):
|
||||||
# We don't need quote_name_unless_alias() here, since these are all
|
# We don't need quote_name_unless_alias() here, since these are all
|
||||||
# going to be column names (so we can avoid the extra overhead).
|
# going to be column names (so we can avoid the extra overhead).
|
||||||
qn = self.connection.ops.quote_name
|
qn = self.connection.ops.quote_name
|
||||||
opts = self.query.model._meta
|
opts = self.query.get_meta()
|
||||||
result = ['INSERT INTO %s' % qn(opts.db_table)]
|
result = ['INSERT INTO %s' % qn(opts.db_table)]
|
||||||
|
|
||||||
has_fields = bool(self.query.fields)
|
has_fields = bool(self.query.fields)
|
||||||
|
@ -887,7 +887,7 @@ class SQLInsertCompiler(SQLCompiler):
|
||||||
if self.connection.features.can_return_id_from_insert:
|
if self.connection.features.can_return_id_from_insert:
|
||||||
return self.connection.ops.fetch_returned_insert_id(cursor)
|
return self.connection.ops.fetch_returned_insert_id(cursor)
|
||||||
return self.connection.ops.last_insert_id(cursor,
|
return self.connection.ops.last_insert_id(cursor,
|
||||||
self.query.model._meta.db_table, self.query.model._meta.pk.column)
|
self.query.get_meta().db_table, self.query.get_meta().pk.column)
|
||||||
|
|
||||||
|
|
||||||
class SQLDeleteCompiler(SQLCompiler):
|
class SQLDeleteCompiler(SQLCompiler):
|
||||||
|
@ -992,7 +992,7 @@ class SQLUpdateCompiler(SQLCompiler):
|
||||||
query.bump_prefix()
|
query.bump_prefix()
|
||||||
query.extra = {}
|
query.extra = {}
|
||||||
query.select = []
|
query.select = []
|
||||||
query.add_fields([query.model._meta.pk.name])
|
query.add_fields([query.get_meta().pk.name])
|
||||||
# Recheck the count - it is possible that fiddling with the select
|
# Recheck the count - it is possible that fiddling with the select
|
||||||
# fields above removes tables from the query. Refs #18304.
|
# fields above removes tables from the query. Refs #18304.
|
||||||
count = query.count_active_tables()
|
count = query.count_active_tables()
|
||||||
|
|
|
@ -552,7 +552,7 @@ class Query(object):
|
||||||
field_names, defer = self.deferred_loading
|
field_names, defer = self.deferred_loading
|
||||||
if not field_names:
|
if not field_names:
|
||||||
return
|
return
|
||||||
orig_opts = self.model._meta
|
orig_opts = self.get_meta()
|
||||||
seen = {}
|
seen = {}
|
||||||
must_include = {orig_opts.concrete_model: set([orig_opts.pk])}
|
must_include = {orig_opts.concrete_model: set([orig_opts.pk])}
|
||||||
for field_name in field_names:
|
for field_name in field_names:
|
||||||
|
@ -818,7 +818,7 @@ class Query(object):
|
||||||
alias = self.tables[0]
|
alias = self.tables[0]
|
||||||
self.ref_alias(alias)
|
self.ref_alias(alias)
|
||||||
else:
|
else:
|
||||||
alias = self.join((None, self.model._meta.db_table, None))
|
alias = self.join((None, self.get_meta().db_table, None))
|
||||||
return alias
|
return alias
|
||||||
|
|
||||||
def count_active_tables(self):
|
def count_active_tables(self):
|
||||||
|
@ -906,7 +906,7 @@ class Query(object):
|
||||||
whereas column determination is a later part, and side-effect, of
|
whereas column determination is a later part, and side-effect, of
|
||||||
as_sql()).
|
as_sql()).
|
||||||
"""
|
"""
|
||||||
opts = self.model._meta
|
opts = self.get_meta()
|
||||||
root_alias = self.tables[0]
|
root_alias = self.tables[0]
|
||||||
seen = {None: root_alias}
|
seen = {None: root_alias}
|
||||||
|
|
||||||
|
@ -1624,7 +1624,7 @@ class Query(object):
|
||||||
"Cannot add count col with multiple cols in 'select': %r" % self.select
|
"Cannot add count col with multiple cols in 'select': %r" % self.select
|
||||||
count = self.aggregates_module.Count(self.select[0].col)
|
count = self.aggregates_module.Count(self.select[0].col)
|
||||||
else:
|
else:
|
||||||
opts = self.model._meta
|
opts = self.get_meta()
|
||||||
if not self.select:
|
if not self.select:
|
||||||
count = self.aggregates_module.Count(
|
count = self.aggregates_module.Count(
|
||||||
(self.join((None, opts.db_table, None)), opts.pk.column),
|
(self.join((None, opts.db_table, None)), opts.pk.column),
|
||||||
|
@ -1732,7 +1732,7 @@ class Query(object):
|
||||||
field_names = set(field_names)
|
field_names = set(field_names)
|
||||||
if 'pk' in field_names:
|
if 'pk' in field_names:
|
||||||
field_names.remove('pk')
|
field_names.remove('pk')
|
||||||
field_names.add(self.model._meta.pk.name)
|
field_names.add(self.get_meta().pk.name)
|
||||||
|
|
||||||
if defer:
|
if defer:
|
||||||
# Remove any existing deferred names from the current set before
|
# Remove any existing deferred names from the current set before
|
||||||
|
|
|
@ -41,12 +41,12 @@ class DeleteQuery(Query):
|
||||||
lot of values in pk_list.
|
lot of values in pk_list.
|
||||||
"""
|
"""
|
||||||
if not field:
|
if not field:
|
||||||
field = self.model._meta.pk
|
field = self.get_meta().pk
|
||||||
for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
|
for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
|
||||||
where = self.where_class()
|
where = self.where_class()
|
||||||
where.add((Constraint(None, field.column, field), 'in',
|
where.add((Constraint(None, field.column, field), 'in',
|
||||||
pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]), AND)
|
pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]), AND)
|
||||||
self.do_query(self.model._meta.db_table, where, using=using)
|
self.do_query(self.get_meta().db_table, where, using=using)
|
||||||
|
|
||||||
def delete_qs(self, query, using):
|
def delete_qs(self, query, using):
|
||||||
"""
|
"""
|
||||||
|
@ -112,7 +112,7 @@ class UpdateQuery(Query):
|
||||||
related_updates=self.related_updates.copy(), **kwargs)
|
related_updates=self.related_updates.copy(), **kwargs)
|
||||||
|
|
||||||
def update_batch(self, pk_list, values, using):
|
def update_batch(self, pk_list, values, using):
|
||||||
pk_field = self.model._meta.pk
|
pk_field = self.get_meta().pk
|
||||||
self.add_update_values(values)
|
self.add_update_values(values)
|
||||||
for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
|
for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
|
||||||
self.where = self.where_class()
|
self.where = self.where_class()
|
||||||
|
@ -129,7 +129,7 @@ class UpdateQuery(Query):
|
||||||
"""
|
"""
|
||||||
values_seq = []
|
values_seq = []
|
||||||
for name, val in six.iteritems(values):
|
for name, val in six.iteritems(values):
|
||||||
field, model, direct, m2m = self.model._meta.get_field_by_name(name)
|
field, model, direct, m2m = self.get_meta().get_field_by_name(name)
|
||||||
if not direct or m2m:
|
if not direct or m2m:
|
||||||
raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field)
|
raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field)
|
||||||
if model:
|
if model:
|
||||||
|
@ -236,7 +236,7 @@ class DateQuery(Query):
|
||||||
)
|
)
|
||||||
except FieldError:
|
except FieldError:
|
||||||
raise FieldDoesNotExist("%s has no field named '%s'" % (
|
raise FieldDoesNotExist("%s has no field named '%s'" % (
|
||||||
self.model._meta.object_name, field_name
|
self.get_meta().object_name, field_name
|
||||||
))
|
))
|
||||||
field = result[0]
|
field = result[0]
|
||||||
self._check_field(field) # overridden in DateTimeQuery
|
self._check_field(field) # overridden in DateTimeQuery
|
||||||
|
|
Loading…
Reference in New Issue