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