11from itertools import chain
22
33from django .db import NotSupportedError
4- from django .db .models .expressions import Value
54from django .db .models .fields .json import (
65 ContainedBy ,
76 DataContains ,
1716 KeyTransformNumericLookupMixin ,
1817)
1918
20- from ..lookups import builtin_lookup , is_constant_value , is_simple_column
19+ from ..lookups import builtin_lookup
2120from ..query_utils import process_lhs , process_rhs
2221
2322
@@ -72,23 +71,13 @@ def _has_key_predicate(path, root_column=None, negated=False, as_path=False):
7271 return result
7372
7473
75- def has_key_lookup_rhs_check (rhs ):
76- for key in rhs :
77- if not is_constant_value (key ):
78- return False
79- value = key .value if isinstance (key , Value ) else key
80- if isinstance (value , str ) and "." in value :
81- return False
82- return True
83-
84-
8574def has_key_lookup (self , compiler , connection , as_path = False ):
8675 """Return MQL to check for the existence of a key."""
8776 rhs = self .rhs
8877 lhs = process_lhs (self , compiler , connection )
8978 if not isinstance (rhs , (list , tuple )):
9079 rhs = [rhs ]
91- as_path = as_path and is_simple_column ( self .lhs ) and has_key_lookup_rhs_check ( rhs )
80+ as_path = as_path and self .is_simple_expression ( )
9281 paths = []
9382 # Transform any "raw" keys into KeyTransforms to allow consistent handling
9483 # in the code that follows.
@@ -132,7 +121,7 @@ def key_transform(self, compiler, connection, as_path=False):
132121 while isinstance (previous , KeyTransform ):
133122 key_transforms .insert (0 , previous .key_name )
134123 previous = previous .lhs
135- if as_path and is_simple_column (self .lhs ):
124+ if as_path and self . is_simple_column (self .lhs ):
136125 lhs_mql = previous .as_mql (compiler , connection , as_path = True )
137126 return build_json_mql_path (lhs_mql , key_transforms , as_path = True )
138127 # Collect all key transforms in order.
@@ -147,7 +136,7 @@ def key_transform_in(self, compiler, connection, as_path=False):
147136 Return MQL to check if a JSON path exists and that its values are in the
148137 set of specified values (rhs).
149138 """
150- if as_path and is_simple_column ( self .lhs ) and is_constant_value ( self . rhs ):
139+ if as_path and self .is_simple_expression ( ):
151140 return builtin_lookup (self , compiler , connection , as_path = True )
152141
153142 lhs_mql = process_lhs (self , compiler , connection )
@@ -175,7 +164,7 @@ def key_transform_is_null(self, compiler, connection, as_path=False):
175164
176165 Reference: https://code.djangoproject.com/ticket/32252
177166 """
178- if as_path and is_simple_column ( self .lhs ) and is_constant_value ( self . rhs ):
167+ if as_path and self .is_simple_expression ( ):
179168 lhs_mql = process_lhs (self , compiler , connection , as_path = True )
180169 rhs_mql = process_rhs (self , compiler , connection )
181170 return _has_key_predicate (lhs_mql , None , negated = rhs_mql , as_path = True )
@@ -195,7 +184,7 @@ def key_transform_numeric_lookup_mixin(self, compiler, connection, as_path=False
195184 Return MQL to check if the field exists (i.e., is not "missing" or "null")
196185 and that the field matches the given numeric lookup expression.
197186 """
198- if is_simple_column ( self . lhs ) and is_constant_value ( self .rhs ) and as_path :
187+ if as_path and self .is_simple_expression () :
199188 return builtin_lookup (self , compiler , connection , as_path = True )
200189
201190 lhs = process_lhs (self , compiler , connection , as_path = False )
@@ -209,7 +198,7 @@ def key_transform_numeric_lookup_mixin(self, compiler, connection, as_path=False
209198
210199
211200def key_transform_exact (self , compiler , connection , as_path = False ):
212- if is_simple_column ( self . lhs ) and is_constant_value ( self .rhs ) and as_path :
201+ if as_path and self .is_simple_expression () :
213202 lhs_mql = process_lhs (self , compiler , connection , as_path = True )
214203 return {
215204 "$and" : [
0 commit comments