2
2
3
3
from pymongo import ASCENDING , DESCENDING
4
4
5
+ from django .db import UnsupportedDatabaseOperation
5
6
from django .db .models import F
6
7
from django .db .models .sql .datastructures import FullResultSet , EmptyResultSet
7
8
@@ -43,10 +44,13 @@ def get_filters(self, where):
43
44
pass
44
45
return filters
45
46
46
- def make_atom (self , lhs , lookup_type , value_annotation , params_or_value , negated ):
47
+ def make_atom (self , lhs , lookup_type , value_annotation , params_or_value ,
48
+ negated ):
47
49
assert lookup_type in self .LOOKUP_TYPES , lookup_type
48
50
if hasattr (lhs , "process" ):
49
- lhs , params = lhs .process (lookup_type , params_or_value , self .connection )
51
+ lhs , params = lhs .process (
52
+ lookup_type , params_or_value , self .connection
53
+ )
50
54
else :
51
55
params = Field ().get_db_prep_lookup (lookup_type , params_or_value ,
52
56
connection = self .connection , prepared = True )
@@ -56,7 +60,8 @@ def make_atom(self, lhs, lookup_type, value_annotation, params_or_value, negated
56
60
if column == self .query .model ._meta .pk .column :
57
61
column = "_id"
58
62
59
- return column , self .LOOKUP_TYPES [lookup_type ](params , value_annotation , negated )
63
+ val = self .LOOKUP_TYPES [lookup_type ](params , value_annotation , negated )
64
+ return column , val
60
65
61
66
def negate (self , k , v ):
62
67
# Regex lookups are of the form {"field": re.compile("pattern") and
@@ -79,14 +84,18 @@ def get_fields(self, aggregates):
79
84
return None
80
85
81
86
def build_query (self , aggregates = False ):
82
- assert len ([a for a in self .query .alias_map if self .query .alias_refcount [a ]]) <= 1
87
+ if len ([a for a in self .query .alias_map if self .query .alias_refcount [a ]]) > 1 :
88
+ raise UnsupportedDatabaseOperation ("MongoDB does not support "
89
+ "operations across relations." )
90
+ if self .query .extra :
91
+ raise UnsupportedDatabaseOperation ("MongoDB does not support extra()." )
83
92
assert not self .query .distinct
84
- assert not self .query .extra
85
93
assert not self .query .having
86
94
87
95
filters = self .get_filters (self .query .where )
88
96
fields = self .get_fields (aggregates = aggregates )
89
- cursor = self .connection .db [self .query .model ._meta .db_table ].find (filters , fields = fields )
97
+ collection = self .connection .db [self .query .model ._meta .db_table ]
98
+ cursor = collection .find (filters , fields = fields )
90
99
if self .query .order_by :
91
100
cursor = cursor .sort ([
92
101
(ordering .lstrip ("-" ), DESCENDING if ordering .startswith ("-" ) else ASCENDING )
@@ -125,14 +134,19 @@ def has_results(self):
125
134
return True
126
135
127
136
def get_aggregates (self ):
137
+ if len (self .query .aggregates ) != 1 :
138
+ raise UnsupportedDatabaseOperation ("MongoDB doesn't support "
139
+ "multiple aggregates in a single query." )
128
140
assert len (self .query .aggregates ) == 1
129
141
agg = self .query .aggregates .values ()[0 ]
130
- assert (
131
- isinstance (agg , self .query .aggregates_module .Count ) and (
132
- agg .col == "*" or
133
- isinstance (agg .col , tuple ) and agg .col == (self .query .model ._meta .db_table , self .query .model ._meta .pk .column )
134
- )
135
- )
142
+ if not isinstance (agg , self .query .aggregates_module .Count ):
143
+ raise UnsupportedDatabaseOperation ("MongoDB does not support "
144
+ "aggregates other than Count." )
145
+ opts = self .query .model ._meta
146
+ if not (agg .col == "*" or agg .col == (opts .db_table , opts .pk .column )):
147
+ raise UnsupportedDatabaseOperation ("MongoDB does not support "
148
+ "aggregation over fields besides the primary key." )
149
+
136
150
return [self .build_query (aggregates = True ).count ()]
137
151
138
152
@@ -152,8 +166,7 @@ class SQLUpdateCompiler(SQLCompiler):
152
166
def update (self , result_type ):
153
167
# TODO: more asserts
154
168
filters = self .get_filters (self .query .where )
155
- # TODO: Don't use set for everything, use INC and such where
156
- # appropriate.
169
+
157
170
vals = {}
158
171
for field , o , value in self .query .values :
159
172
if hasattr (value , "evaluate" ):
0 commit comments