1
1
from functools import reduce , wraps
2
2
from operator import add as add_operator
3
3
4
- from django .core .exceptions import EmptyResultSet , FullResultSet , FieldDoesNotExist
4
+ from django .core .exceptions import EmptyResultSet , FieldDoesNotExist , FullResultSet
5
5
from django .db import DatabaseError , IntegrityError , NotSupportedError , connections
6
6
from django .db .models import QuerySet
7
7
from django .db .models .expressions import Case , Col , When
8
8
from django .db .models .functions import Mod
9
9
from django .db .models .lookups import Exact
10
- from django .db .models .query import RawQuerySet , RawModelIterable
10
+ from django .db .models .query import RawModelIterable , RawQuerySet
11
11
from django .db .models .sql .constants import INNER
12
12
from django .db .models .sql .datastructures import Join
13
13
from django .db .models .sql .query import RawQuery
@@ -308,7 +308,6 @@ def register_nodes():
308
308
309
309
310
310
class MongoQuerySet (QuerySet ):
311
-
312
311
def raw_mql (self , raw_query , params = (), translations = None , using = None ):
313
312
return MongoRawQuerySet (
314
313
raw_query ,
@@ -320,7 +319,6 @@ def raw_mql(self, raw_query, params=(), translations=None, using=None):
320
319
321
320
322
321
class MongoRawQuery (RawQuery ):
323
-
324
322
def __init__ (self , sql , using , model , params = ()):
325
323
super ().__init__ (sql , using , params )
326
324
self .model = model
@@ -352,7 +350,14 @@ def __init__(
352
350
using = None ,
353
351
hints = None ,
354
352
):
355
- super ().__init__ (raw_query , model = model , query = query , using = using , hints = hints , translations = translations )
353
+ super ().__init__ (
354
+ raw_query ,
355
+ model = model ,
356
+ query = query ,
357
+ using = using ,
358
+ hints = hints ,
359
+ translations = translations ,
360
+ )
356
361
self .query = query or MongoRawQuery (
357
362
sql = raw_query , using = self .db , model = self .model , params = params
358
363
)
@@ -362,17 +367,11 @@ def iterator(self):
362
367
363
368
def resolve_model_init_order (self , columns ):
364
369
"""Resolve the init field names and value positions."""
365
- model_init_fields = [
366
- f for f in self .model ._meta .fields if f .column in columns
367
- ]
370
+ model_init_fields = [f for f in self .model ._meta .fields if f .column in columns ]
368
371
annotation_fields = [
369
- (column , pos )
370
- for pos , column in enumerate (columns )
371
- if column not in self .model_fields
372
- ]
373
- model_init_order = [
374
- columns .index (f .column ) for f in model_init_fields
372
+ (column , pos ) for pos , column in enumerate (columns ) if column not in self .model_fields
375
373
]
374
+ model_init_order = [columns .index (f .column ) for f in model_init_fields ]
376
375
model_init_names = [f .attname for f in model_init_fields ]
377
376
return model_init_names , model_init_order , annotation_fields
378
377
@@ -401,9 +400,7 @@ def __iter__(self):
401
400
) = self .queryset .resolve_model_init_order (columns )
402
401
model_cls = self .queryset .model
403
402
if model_cls ._meta .pk .attname not in model_init_names :
404
- raise FieldDoesNotExist (
405
- "Raw query must include the primary key"
406
- )
403
+ raise FieldDoesNotExist ("Raw query must include the primary key" )
407
404
fields = [self .queryset .model_fields .get (c ) for c in self .queryset .columns ]
408
405
converters = compiler .get_converters (
409
406
[f .get_col (f .model ._meta .db_table ) if f else None for f in fields ]
0 commit comments