@@ -13,6 +13,19 @@ def raw_aggregate(self, pipeline, using=None):
1313 return RawQuerySet (pipeline , model = self .model , using = using )
1414
1515
16+ class RawQuerySet (BaseRawQuerySet ):
17+ def __init__ (self , pipeline , model = None , using = None ):
18+ super ().__init__ (pipeline , model = model , using = using )
19+ self .query = RawQuery (pipeline , using = self .db , model = self .model )
20+ # Override the superclass's columns property which relies on PEP 249's
21+ # cursor.description. Instead, RawModelIterable will set the columns
22+ # based on the keys in the first result.
23+ self .columns = None
24+
25+ def iterator (self ):
26+ yield from RawModelIterable (self )
27+
28+
1629class RawQuery (BaseRawQuery ):
1730 def __init__ (self , pipeline , using , model ):
1831 self .pipeline = pipeline
@@ -28,24 +41,11 @@ def __str__(self):
2841 return str (self .pipeline )
2942
3043
31- class RawQuerySet (BaseRawQuerySet ):
32- def __init__ (self , pipeline , model = None , using = None ):
33- super ().__init__ (pipeline , model = model , using = using )
34- self .query = RawQuery (pipeline , using = self .db , model = self .model )
35- # Override the superclass's columns property which relies on PEP 249's
36- # cursor.description. Instead, RawModelIterable will set the columns
37- # based on the keys in the first result.
38- self .columns = None
39-
40- def iterator (self ):
41- yield from RawModelIterable (self )
42-
43-
4444class RawModelIterable (BaseRawModelIterable ):
4545 def __iter__ (self ):
4646 """
47- This is mostly copied from the superclass except for the part that sets
48- self.queryset.columns from the first document .
47+ This is copied from the superclass except for the part that sets
48+ self.queryset.columns from the first result .
4949 """
5050 db = self .queryset .db
5151 query = self .queryset .query
@@ -55,13 +55,13 @@ def __iter__(self):
5555 try :
5656 # Get the columns from the first result.
5757 try :
58- first_item = next (query_iterator )
58+ first_result = next (query_iterator )
5959 except StopIteration :
6060 # No results.
6161 return
62- self .queryset .columns = list (first_item .keys ())
62+ self .queryset .columns = list (first_result .keys ())
6363 # Reset the iterator to include the first item.
64- query_iterator = self ._make_result (chain ([first_item ], query_iterator ))
64+ query_iterator = self ._make_result (chain ([first_result ], query_iterator ))
6565 (
6666 model_init_names ,
6767 model_init_pos ,
@@ -88,5 +88,9 @@ def __iter__(self):
8888 query .cursor .close ()
8989
9090 def _make_result (self , query ):
91+ """
92+ Convert documents (dictionaries) to tuples as expected by the rest
93+ of __iter__().
94+ """
9195 for result in query :
92- yield list (result .values ())
96+ yield tuple (result .values ())
0 commit comments