Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

queryset-refactor: Make sure the right list of fields is passed to the

Query.resolve_columns() method for those backends that provide it (e.g. Oracle).

Refs #7088


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7469 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3933aff6ccc05510deacd429b0c0d3cd8c909009 1 parent bb132aa
Malcolm Tredinnick authored April 26, 2008
23  django/db/models/sql/query.py
@@ -54,6 +54,8 @@ def __init__(self, model, connection, where=WhereNode):
54 54
         self.standard_ordering = True
55 55
         self.ordering_aliases = []
56 56
         self.start_meta = None
  57
+        self.select_fields = []
  58
+        self.related_select_fields = []
57 59
 
58 60
         # SQL-related attributes
59 61
         self.select = []
@@ -142,6 +144,8 @@ def clone(self, klass=None, **kwargs):
142 144
         obj.standard_ordering = self.standard_ordering
143 145
         obj.ordering_aliases = []
144 146
         obj.start_meta = self.start_meta
  147
+        obj.select_fields = self.select_fields[:]
  148
+        obj.related_select_fields = self.related_select_fields[:]
145 149
         obj.select = self.select[:]
146 150
         obj.tables = self.tables[:]
147 151
         obj.where = deepcopy(self.where)
@@ -169,8 +173,12 @@ def results_iter(self):
169 173
         """
170 174
         Returns an iterator over the results from executing this query.
171 175
         """
172  
-        fields = self.model._meta.fields
173 176
         resolve_columns = hasattr(self, 'resolve_columns')
  177
+        if resolve_columns:
  178
+            if self.select_fields:
  179
+                fields = self.select_fields + self.related_select_fields
  180
+            else:
  181
+                fields = self.model._meta.fields
174 182
         for rows in self.execute_sql(MULTI):
175 183
             for row in rows:
176 184
                 if resolve_columns:
@@ -187,6 +195,7 @@ def get_count(self):
187 195
         obj.clear_limits()
188 196
         obj.select_related = False
189 197
         obj.related_select_cols = []
  198
+        obj.related_select_fields = []
190 199
         if obj.distinct and len(obj.select) > 1:
191 200
             obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
192 201
                     distinct=False)
@@ -199,7 +208,8 @@ def get_count(self):
199 208
         number = data[0]
200 209
 
201 210
         # Apply offset and limit constraints manually, since using LIMIT/OFFSET
202  
-        # in SQL doesn't change the COUNT output.
  211
+        # in SQL (in variants that provide them) doesn't change the COUNT
  212
+        # output.
203 213
         number = max(0, number - self.low_mark)
204 214
         if self.high_mark:
205 215
             number = min(number, self.high_mark - self.low_mark)
@@ -333,6 +343,7 @@ def combine(self, rhs, connector):
333 343
                 item = deepcopy(col)
334 344
                 item.relabel_aliases(change_map)
335 345
                 self.select.append(item)
  346
+        self.select_fields = rhs.select_fields[:]
336 347
         self.extra_select = rhs.extra_select.copy()
337 348
         self.extra_tables = rhs.extra_tables
338 349
         self.extra_where = rhs.extra_where
@@ -835,6 +846,7 @@ def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
835 846
             opts = self.get_meta()
836 847
             root_alias = self.get_initial_alias()
837 848
             self.related_select_cols = []
  849
+            self.related_select_fields = []
838 850
         if not used:
839 851
             used = set()
840 852
 
@@ -869,6 +881,7 @@ def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
869 881
             used.add(alias)
870 882
             self.related_select_cols.extend([(alias, f2.column)
871 883
                     for f2 in f.rel.to._meta.fields])
  884
+            self.related_select_fields.extend(f.rel.to._meta.fields)
872 885
             if restricted:
873 886
                 next = requested.get(f.name, {})
874 887
             else:
@@ -1237,7 +1250,7 @@ def add_fields(self, field_names, allow_m2m=True):
1237 1250
         opts = self.get_meta()
1238 1251
         try:
1239 1252
             for name in field_names:
1240  
-                u1, target, u2, joins, u3 = self.setup_joins(
  1253
+                field, target, u2, joins, u3 = self.setup_joins(
1241 1254
                         name.split(LOOKUP_SEP), opts, alias, False, allow_m2m,
1242 1255
                         True)
1243 1256
                 final_alias = joins[-1]
@@ -1254,6 +1267,7 @@ def add_fields(self, field_names, allow_m2m=True):
1254 1267
                     # doing unnecessary left outer joins here.
1255 1268
                     self.promote_alias(join)
1256 1269
                 self.select.append((final_alias, col))
  1270
+                self.select_fields.append(field)
1257 1271
         except MultiJoin:
1258 1272
             raise FieldError("Invalid field name: '%s'" % name)
1259 1273
         except FieldError:
@@ -1322,6 +1336,7 @@ def add_count_column(self):
1322 1336
             # level.
1323 1337
             self.distinct = False
1324 1338
         self.select = [select]
  1339
+        self.select_fields = [None]
1325 1340
         self.extra_select = {}
1326 1341
         self.extra_select_params = ()
1327 1342
 
@@ -1338,6 +1353,7 @@ def add_select_related(self, fields):
1338 1353
                 d = d.setdefault(part, {})
1339 1354
         self.select_related = field_dict
1340 1355
         self.related_select_cols = []
  1356
+        self.related_select_fields = []
1341 1357
 
1342 1358
     def add_extra(self, select, select_params, where, params, tables, order_by):
1343 1359
         """
@@ -1390,6 +1406,7 @@ def set_start(self, start):
1390 1406
                 start.split(LOOKUP_SEP), opts, alias, False)
1391 1407
         alias = joins[last[-1]]
1392 1408
         self.select = [(alias, self.alias_map[alias][RHS_JOIN_COL])]
  1409
+        self.select_fields = [field]
1393 1410
         self.start_meta = opts
1394 1411
 
1395 1412
         # The call to setup_joins add an extra reference to everything in
1  django/db/models/sql/subqueries.py
@@ -348,6 +348,7 @@ def add_date_select(self, column, lookup_type, order='ASC'):
348 348
         select = Date((alias, column), lookup_type,
349 349
                 self.connection.ops.date_trunc_sql)
350 350
         self.select = [select]
  351
+        self.select_fields = [None]
351 352
         self.distinct = True
352 353
         self.order_by = order == 'ASC' and [1] or [-1]
353 354
 

0 notes on commit 3933aff

Please sign in to comment.
Something went wrong with that request. Please try again.