Skip to content

Commit f9fbef8

Browse files
committed
INTPYTHON-348 add support for QuerySet.raw_mql()
1 parent e50d099 commit f9fbef8

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

django_mongodb/query.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -325,56 +325,28 @@ def __init__(self, sql, using, model, params=()):
325325
self.using = using
326326
self.cursor = None
327327
self.model = model
328-
329-
# Mirror some properties of a normal query so that
330-
# the compiler can be used to process results.
331328
self.low_mark, self.high_mark = 0, None # Used for offset/limit
332329
self.extra_select = {}
333330
self.annotation_select = {}
334331

335332
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-
339333
self.cursor = self._execute_query()
340-
341334
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.
344335
result = list(self.cursor)
345336
else:
346337
result = self.cursor
347-
348338
return iter(result)
349339

350340
def _execute_query(self):
351341
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-
367342
collection = connection.get_collection(self.model._meta.db_table)
368-
369343
return collection.aggregate(self.sql)
370344

371345
def get_columns(self):
372346
if self.cursor is None:
373347
self._execute_query()
374348
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]
378350

379351

380352
class MongoRawQuerySet(RawQuerySet):
@@ -396,3 +368,17 @@ def __init__(
396368
)
397369
self._result_cache = None
398370
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

Comments
 (0)