1
1
from django .core .exceptions import EmptyResultSet , FullResultSet
2
2
from django .db import NotSupportedError
3
3
from django .db .models import Index
4
+ from django .db .models .expressions import Col
5
+ from django .db .models .lookups import BuiltinLookup
4
6
from django .db .models .sql .query import Query
5
7
from django .db .models .sql .where import AND , XOR , WhereNode
6
8
9
+ from .query_utils import process_rhs
10
+
7
11
8
12
def _get_condition_mql (self , model , schema_editor ):
9
13
"""Analogous to Index._get_condition_sql()."""
@@ -13,6 +17,14 @@ def _get_condition_mql(self, model, schema_editor):
13
17
return where .as_mql_idx (compiler , schema_editor .connection )
14
18
15
19
20
+ def builtin_lookup_idx (self , compiler , connection ):
21
+ if not isinstance (self .lhs , Col ):
22
+ raise NotSupportedError ("Expressions as indexes are not supported." )
23
+ lhs_mql = self .lhs .target .column
24
+ value = process_rhs (self , compiler , connection )
25
+ return connection .mongo_operators_idx [self .lookup_name ](lhs_mql , value )
26
+
27
+
16
28
def where_node_idx (self , compiler , connection ):
17
29
if self .connector == AND :
18
30
full_needed , empty_needed = len (self .children ), 1
@@ -57,5 +69,6 @@ def where_node_idx(self, compiler, connection):
57
69
58
70
59
71
def register_indexes ():
72
+ BuiltinLookup .as_mql_idx = builtin_lookup_idx
60
73
Index ._get_condition_mql = _get_condition_mql
61
74
WhereNode .as_mql_idx = where_node_idx
0 commit comments