|
1 | 1 | import difflib |
2 | 2 |
|
3 | 3 | from django.core.exceptions import FieldDoesNotExist |
4 | | -from django.db import models |
5 | 4 | from django.db.models import Field |
6 | 5 | from django.db.models.expressions import Col |
7 | 6 | from django.db.models.lookups import Lookup, Transform |
@@ -66,30 +65,10 @@ def get_transform(self, name): |
66 | 65 | @EmbeddedModelArrayField.register_lookup |
67 | 66 | class EMFArrayExact(EMFExact): |
68 | 67 | def as_mql(self, compiler, connection): |
69 | | - lhs_mql = process_lhs(self, compiler, connection) |
| 68 | + if not isinstance(self.lhs, KeyTransform): |
| 69 | + raise ValueError("error") |
| 70 | + lhs_mql, inner_lhs_mql = process_lhs(self, compiler, connection) |
70 | 71 | value = process_rhs(self, compiler, connection) |
71 | | - if isinstance(self.lhs, KeyTransform): |
72 | | - lhs_mql, inner_lhs_mql = lhs_mql |
73 | | - else: |
74 | | - inner_lhs_mql = "$$item" |
75 | | - if isinstance(value, models.Model): |
76 | | - value, emf_data = self.model_to_dict(value) |
77 | | - # Get conditions for any nested EmbeddedModelFields. |
78 | | - conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)}) |
79 | | - return { |
80 | | - "$anyElementTrue": { |
81 | | - "$ifNull": [ |
82 | | - { |
83 | | - "$map": { |
84 | | - "input": lhs_mql, |
85 | | - "as": "item", |
86 | | - "in": {"$and": conditions}, |
87 | | - } |
88 | | - }, |
89 | | - [], |
90 | | - ] |
91 | | - } |
92 | | - } |
93 | 72 | return { |
94 | 73 | "$anyElementTrue": { |
95 | 74 | "$ifNull": [ |
@@ -125,43 +104,22 @@ def process_rhs(self, compiler, connection): |
125 | 104 | return None, [get_db_prep_value(v, connection, prepared=True) for v in values] |
126 | 105 |
|
127 | 106 | def as_mql(self, compiler, connection): |
128 | | - lhs_mql = process_lhs(self, compiler, connection) |
129 | | - values = process_rhs(self, compiler, connection) |
130 | 107 | # Querying a subfield within the array elements (via nested KeyTransform). |
131 | 108 | # Replicates MongoDB's implicit ANY-match by mapping over the array and applying |
132 | 109 | # `$in` on the subfield. |
133 | | - if isinstance(self.lhs, KeyTransform): |
134 | | - lhs_mql, inner_lhs_mql = lhs_mql |
135 | | - return { |
136 | | - "$anyElementTrue": { |
137 | | - "$ifNull": [ |
138 | | - { |
139 | | - "$map": { |
140 | | - "input": lhs_mql, |
141 | | - "as": "item", |
142 | | - "in": {"$in": [inner_lhs_mql, values]}, |
143 | | - } |
144 | | - }, |
145 | | - [], |
146 | | - ] |
147 | | - } |
148 | | - } |
149 | | - conditions = [] |
150 | | - inner_lhs_mql = "$$item" |
151 | | - # Querying full embedded documents in the array. |
152 | | - # Builds `$or` conditions and maps them over the array to match any full document. |
153 | | - for value in values: |
154 | | - value, emf_data = self.model_to_dict(value) |
155 | | - # Get conditions for any nested EmbeddedModelFields. |
156 | | - conditions.append({"$and": self.get_conditions({inner_lhs_mql: (value, emf_data)})}) |
| 110 | + if not isinstance(self.lhs, KeyTransform): |
| 111 | + raise ValueError() |
| 112 | + lhs_mql = process_lhs(self, compiler, connection) |
| 113 | + values = process_rhs(self, compiler, connection) |
| 114 | + lhs_mql, inner_lhs_mql = lhs_mql |
157 | 115 | return { |
158 | 116 | "$anyElementTrue": { |
159 | 117 | "$ifNull": [ |
160 | 118 | { |
161 | 119 | "$map": { |
162 | 120 | "input": lhs_mql, |
163 | 121 | "as": "item", |
164 | | - "in": {"$or": conditions}, |
| 122 | + "in": {"$in": [inner_lhs_mql, values]}, |
165 | 123 | } |
166 | 124 | }, |
167 | 125 | [], |
|
0 commit comments