From 74f3884ae04ea57baee9b04ab0b5658a97cfd296 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Mon, 13 May 2013 12:40:39 -0700 Subject: [PATCH] Fixed #20413 - Respect Query.get_meta() --- django/contrib/gis/db/models/sql/compiler.py | 4 ++-- django/db/models/query.py | 2 +- django/db/models/sql/compiler.py | 24 ++++++++++---------- django/db/models/sql/query.py | 10 ++++---- django/db/models/sql/subqueries.py | 10 ++++---- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/django/contrib/gis/db/models/sql/compiler.py b/django/contrib/gis/db/models/sql/compiler.py index b488f59362..1f81dbd376 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -121,7 +121,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): """ result = [] if opts is None: - opts = self.query.model._meta + opts = self.get_meta() aliases = set() only_load = self.deferred_to_columns() 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 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), self.connection.ops.quote_name(column or field.column)) diff --git a/django/db/models/query.py b/django/db/models/query.py index 337049e2ff..d3763d3934 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -869,7 +869,7 @@ class QuerySet(object): """ if self.query.extra_order_by or self.query.order_by: 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 else: return False diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 018fc098ea..7b7c430420 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -32,7 +32,7 @@ class SQLCompiler(object): # cleaned. We are not using a clone() of the query here. """ 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 self.query.included_inherited_models): self.query.setup_inherited_models() @@ -260,7 +260,7 @@ class SQLCompiler(object): """ result = [] if opts is None: - opts = self.query.model._meta + opts = self.query.get_meta() qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name aliases = set() @@ -309,7 +309,7 @@ class SQLCompiler(object): qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name result = [] - opts = self.query.model._meta + opts = self.query.get_meta() for name in self.query.distinct_fields: parts = name.split(LOOKUP_SEP) @@ -338,7 +338,7 @@ class SQLCompiler(object): ordering = self.query.order_by else: ordering = (self.query.order_by - or self.query.model._meta.ordering + or self.query.get_meta().ordering or []) qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name @@ -388,7 +388,7 @@ class SQLCompiler(object): # 'col' is of the form 'field' or 'field1__field2' or # '-field1__field2__field', etc. 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: if (table, col) not in processed_pairs: elt = '%s.%s' % (qn(table), qn2(col)) @@ -556,10 +556,10 @@ class SQLCompiler(object): select_cols = self.query.select + self.query.related_select_cols # Just the column, not the fields. 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): 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 = [] seen = set() @@ -716,14 +716,14 @@ class SQLCompiler(object): if self.query.select: fields = [f.field for f in self.query.select] 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] # If the field was deferred, exclude it from being passed # into `resolve_columns` because it wasn't selected. only_load = self.deferred_to_columns() 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 f.column in only_load[db_table]] 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 # going to be column names (so we can avoid the extra overhead). qn = self.connection.ops.quote_name - opts = self.query.model._meta + opts = self.query.get_meta() result = ['INSERT INTO %s' % qn(opts.db_table)] has_fields = bool(self.query.fields) @@ -887,7 +887,7 @@ class SQLInsertCompiler(SQLCompiler): if self.connection.features.can_return_id_from_insert: return self.connection.ops.fetch_returned_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): @@ -992,7 +992,7 @@ class SQLUpdateCompiler(SQLCompiler): query.bump_prefix() query.extra = {} 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 # fields above removes tables from the query. Refs #18304. count = query.count_active_tables() diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index acb9f248d6..6db2bf6e12 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -552,7 +552,7 @@ class Query(object): field_names, defer = self.deferred_loading if not field_names: return - orig_opts = self.model._meta + orig_opts = self.get_meta() seen = {} must_include = {orig_opts.concrete_model: set([orig_opts.pk])} for field_name in field_names: @@ -818,7 +818,7 @@ class Query(object): alias = self.tables[0] self.ref_alias(alias) else: - alias = self.join((None, self.model._meta.db_table, None)) + alias = self.join((None, self.get_meta().db_table, None)) return alias def count_active_tables(self): @@ -906,7 +906,7 @@ class Query(object): whereas column determination is a later part, and side-effect, of as_sql()). """ - opts = self.model._meta + opts = self.get_meta() root_alias = self.tables[0] seen = {None: root_alias} @@ -1624,7 +1624,7 @@ class Query(object): "Cannot add count col with multiple cols in 'select': %r" % self.select count = self.aggregates_module.Count(self.select[0].col) else: - opts = self.model._meta + opts = self.get_meta() if not self.select: count = self.aggregates_module.Count( (self.join((None, opts.db_table, None)), opts.pk.column), @@ -1732,7 +1732,7 @@ class Query(object): field_names = set(field_names) if 'pk' in field_names: field_names.remove('pk') - field_names.add(self.model._meta.pk.name) + field_names.add(self.get_meta().pk.name) if defer: # Remove any existing deferred names from the current set before diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 78727e394a..fae42b4be2 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -41,12 +41,12 @@ class DeleteQuery(Query): lot of values in pk_list. """ 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): where = self.where_class() where.add((Constraint(None, field.column, field), 'in', 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): """ @@ -112,7 +112,7 @@ class UpdateQuery(Query): related_updates=self.related_updates.copy(), **kwargs) 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) for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() @@ -129,7 +129,7 @@ class UpdateQuery(Query): """ values_seq = [] 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: raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field) if model: @@ -236,7 +236,7 @@ class DateQuery(Query): ) except FieldError: 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] self._check_field(field) # overridden in DateTimeQuery