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