@@ -325,56 +325,28 @@ def __init__(self, sql, using, model, params=()):
325
325
self .using = using
326
326
self .cursor = None
327
327
self .model = model
328
-
329
- # Mirror some properties of a normal query so that
330
- # the compiler can be used to process results.
331
328
self .low_mark , self .high_mark = 0 , None # Used for offset/limit
332
329
self .extra_select = {}
333
330
self .annotation_select = {}
334
331
335
332
def __iter__ (self ):
336
- # Always execute a new query for a new iterator.
337
- # This could be optimized with a cache at the expense of RAM.
338
-
339
333
self .cursor = self ._execute_query ()
340
-
341
334
if not connections [self .using ].features .can_use_chunked_reads :
342
- # If the database can't use chunked reads we need to make sure we
343
- # evaluate the entire query up front.
344
335
result = list (self .cursor )
345
336
else :
346
337
result = self .cursor
347
-
348
338
return iter (result )
349
339
350
340
def _execute_query (self ):
351
341
connection = connections [self .using ]
352
- # params_type = self.params_type
353
- # adapter = connection.ops.adapt_unknown_value
354
-
355
- # if params_type is tuple:
356
- # params = tuple(adapter(val) for val in self.params)
357
- # elif params_type is dict:
358
- # params = {key: adapter(val) for key, val in self.params.items()}
359
- # elif params_type is None:
360
- # params = None
361
- # else:
362
- # raise RuntimeError("Unexpected params type: %s" % params_type)
363
-
364
- # self.cursor = connection.cursor()
365
- # self.cursor.execute(self.sql, params)
366
-
367
342
collection = connection .get_collection (self .model ._meta .db_table )
368
-
369
343
return collection .aggregate (self .sql )
370
344
371
345
def get_columns (self ):
372
346
if self .cursor is None :
373
347
self ._execute_query ()
374
348
converter = connections [self .using ].introspection .identifier_converter
375
- # self.cursor.description = ""
376
- # return [converter(column_meta[0]) for column_meta in self.cursor.description]
377
- return [converter (column_name ) for column_name in self .model ._meta .get_fields ()]
349
+ return [converter (column_name ) for column_name in self .model ._meta .fields ]
378
350
379
351
380
352
class MongoRawQuerySet (RawQuerySet ):
@@ -396,3 +368,17 @@ def __init__(
396
368
)
397
369
self ._result_cache = None
398
370
self .translations = translations or {}
371
+
372
+ def resolve_model_init_order (self ):
373
+ converter = connections [self .db ].introspection .identifier_converter
374
+ model_init_fields = [
375
+ f for f in self .model ._meta .fields if f .column in [j .column for j in self .columns ]
376
+ ]
377
+ annotation_fields = [
378
+ (column , pos )
379
+ for pos , column in enumerate (self .columns )
380
+ if column not in self .model_fields
381
+ ]
382
+ model_init_order = [self .columns .index (converter (f )) for f in model_init_fields ]
383
+ model_init_names = [f .attname for f in model_init_fields ]
384
+ return model_init_names , model_init_order , annotation_fields
0 commit comments