|
1 |
| -from django.db.models import Field |
| 1 | +import difflib |
| 2 | + |
| 3 | +from django.core.exceptions import FieldDoesNotExist |
| 4 | +from django.db.models import Field, lookups |
| 5 | +from django.db.models.expressions import Col |
2 | 6 | from django.db.models.fields.related import lazy_related_operation
|
| 7 | +from django.db.models.lookups import Lookup, Transform |
3 | 8 |
|
4 | 9 | from .. import forms
|
| 10 | +from ..query_utils import process_lhs, process_rhs |
5 | 11 | from . import EmbeddedModelField
|
6 |
| -from .array import ArrayField |
| 12 | +from .array import ArrayField, ArrayLenTransform |
7 | 13 |
|
8 | 14 |
|
9 | 15 | class EmbeddedModelArrayField(ArrayField):
|
@@ -56,3 +62,199 @@ def formfield(self, **kwargs):
|
56 | 62 | **kwargs,
|
57 | 63 | },
|
58 | 64 | )
|
| 65 | + |
| 66 | + def get_transform(self, name): |
| 67 | + transform = super().get_transform(name) |
| 68 | + if transform: |
| 69 | + return transform |
| 70 | + return KeyTransformFactory(name, self) |
| 71 | + |
| 72 | + def _get_lookup(self, lookup_name): |
| 73 | + lookup = super()._get_lookup(lookup_name) |
| 74 | + if lookup is None or lookup is ArrayLenTransform: |
| 75 | + return lookup |
| 76 | + |
| 77 | + class EmbeddedModelArrayFieldLookups(Lookup): |
| 78 | + def as_mql(self, compiler, connection): |
| 79 | + raise ValueError( |
| 80 | + "Lookups aren't supported on EmbeddedModelArrayField. " |
| 81 | + "Try querying one of its embedded fields instead." |
| 82 | + ) |
| 83 | + |
| 84 | + return EmbeddedModelArrayFieldLookups |
| 85 | + |
| 86 | + |
| 87 | +class _EmbeddedModelArrayOutputField(ArrayField): |
| 88 | + """ |
| 89 | + Represent the output of an EmbeddedModelArrayField when traversed in a |
| 90 | + query path. |
| 91 | +
|
| 92 | + This field is not meant to be used in model definitions. It exists solely |
| 93 | + to support query output resolution. When an EmbeddedModelArrayField is |
| 94 | + accessed in a query, the result should behave like an array of the embedded |
| 95 | + model's target type. |
| 96 | +
|
| 97 | + While it mimics ArrayField's lookup behavior, the way those lookups are |
| 98 | + resolved follows the semantics of EmbeddedModelArrayField rather than |
| 99 | + ArrayField. |
| 100 | + """ |
| 101 | + |
| 102 | + ALLOWED_LOOKUPS = { |
| 103 | + "in", |
| 104 | + "exact", |
| 105 | + "iexact", |
| 106 | + "gt", |
| 107 | + "gte", |
| 108 | + "lt", |
| 109 | + "lte", |
| 110 | + } |
| 111 | + |
| 112 | + def get_lookup(self, name): |
| 113 | + return super().get_lookup(name) if name in self.ALLOWED_LOOKUPS else None |
| 114 | + |
| 115 | + |
| 116 | +class EmbeddedModelArrayFieldBuiltinLookup(Lookup): |
| 117 | + def process_rhs(self, compiler, connection): |
| 118 | + value = self.rhs |
| 119 | + if not self.get_db_prep_lookup_value_is_iterable: |
| 120 | + value = [value] |
| 121 | + # Value must be serialized based on the query target. If querying a |
| 122 | + # subfield inside the array (i.e., a nested KeyTransform), use the |
| 123 | + # output field of the subfield. Otherwise, use the base field of the |
| 124 | + # array itself. |
| 125 | + get_db_prep_value = self.lhs._lhs.output_field.get_db_prep_value |
| 126 | + return None, [ |
| 127 | + v if hasattr(v, "as_mql") else get_db_prep_value(v, connection, prepared=True) |
| 128 | + for v in value |
| 129 | + ] |
| 130 | + |
| 131 | + def as_mql(self, compiler, connection): |
| 132 | + # Querying a subfield within the array elements (via nested |
| 133 | + # KeyTransform). Replicate MongoDB's implicit ANY-match by mapping over |
| 134 | + # the array and applying $in on the subfield. |
| 135 | + lhs_mql = process_lhs(self, compiler, connection) |
| 136 | + inner_lhs_mql = lhs_mql["$ifNull"][0]["$map"]["in"] |
| 137 | + values = process_rhs(self, compiler, connection) |
| 138 | + lhs_mql["$ifNull"][0]["$map"]["in"] = connection.mongo_operators[self.lookup_name]( |
| 139 | + inner_lhs_mql, values |
| 140 | + ) |
| 141 | + return {"$anyElementTrue": lhs_mql} |
| 142 | + |
| 143 | + |
| 144 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 145 | +class EmbeddedModelArrayFieldIn(EmbeddedModelArrayFieldBuiltinLookup, lookups.In): |
| 146 | + pass |
| 147 | + |
| 148 | + |
| 149 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 150 | +class EmbeddedModelArrayFieldExact(EmbeddedModelArrayFieldBuiltinLookup, lookups.Exact): |
| 151 | + pass |
| 152 | + |
| 153 | + |
| 154 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 155 | +class EmbeddedModelArrayFieldIExact(EmbeddedModelArrayFieldBuiltinLookup, lookups.IExact): |
| 156 | + get_db_prep_lookup_value_is_iterable = False |
| 157 | + |
| 158 | + |
| 159 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 160 | +class EmbeddedModelArrayFieldGreaterThan(EmbeddedModelArrayFieldBuiltinLookup, lookups.GreaterThan): |
| 161 | + pass |
| 162 | + |
| 163 | + |
| 164 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 165 | +class EmbeddedModelArrayFieldGreaterThanOrEqual( |
| 166 | + EmbeddedModelArrayFieldBuiltinLookup, lookups.GreaterThanOrEqual |
| 167 | +): |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 172 | +class EmbeddedModelArrayFieldLessThan(EmbeddedModelArrayFieldBuiltinLookup, lookups.LessThan): |
| 173 | + pass |
| 174 | + |
| 175 | + |
| 176 | +@_EmbeddedModelArrayOutputField.register_lookup |
| 177 | +class EmbeddedModelArrayFieldLessThanOrEqual( |
| 178 | + EmbeddedModelArrayFieldBuiltinLookup, lookups.LessThanOrEqual |
| 179 | +): |
| 180 | + pass |
| 181 | + |
| 182 | + |
| 183 | +class KeyTransform(Transform): |
| 184 | + def __init__(self, key_name, array_field, *args, **kwargs): |
| 185 | + super().__init__(*args, **kwargs) |
| 186 | + self.array_field = array_field |
| 187 | + self.key_name = key_name |
| 188 | + # Lookups iterate over the array of embedded models. A virtual column |
| 189 | + # of the queried field's type represents each element. |
| 190 | + column_target = array_field.base_field.embedded_model._meta.get_field(key_name).clone() |
| 191 | + column_name = f"$item.{key_name}" |
| 192 | + column_target.db_column = column_name |
| 193 | + column_target.set_attributes_from_name(column_name) |
| 194 | + self._lhs = Col(None, column_target) |
| 195 | + self._sub_transform = None |
| 196 | + |
| 197 | + def __call__(self, this, *args, **kwargs): |
| 198 | + self._lhs = self._sub_transform(self._lhs, *args, **kwargs) |
| 199 | + return self |
| 200 | + |
| 201 | + def get_lookup(self, name): |
| 202 | + return self.output_field.get_lookup(name) |
| 203 | + |
| 204 | + def get_transform(self, name): |
| 205 | + """ |
| 206 | + Validate that `name` is either a field of an embedded model or am |
| 207 | + allowed lookup on an embedded model's field. |
| 208 | + """ |
| 209 | + # Once the sub-lhs is a transform, all the filters are applied over it. |
| 210 | + # Otherwise get the transform from the nested embedded model field. |
| 211 | + if transform := self._lhs.get_transform(name): |
| 212 | + if isinstance(transform, KeyTransformFactory): |
| 213 | + raise ValueError("Cannot perform multiple levels of array traversal in a query.") |
| 214 | + self._sub_transform = transform |
| 215 | + return self |
| 216 | + output_field = self._lhs.output_field |
| 217 | + # The lookup must be allowed AND a valid lookup for the field. |
| 218 | + allowed_lookups = self.output_field.ALLOWED_LOOKUPS.intersection( |
| 219 | + set(output_field.get_lookups()) |
| 220 | + ) |
| 221 | + suggested_lookups = difflib.get_close_matches(name, allowed_lookups) |
| 222 | + if suggested_lookups: |
| 223 | + suggested_lookups = " or ".join(suggested_lookups) |
| 224 | + suggestion = f", perhaps you meant {suggested_lookups}?" |
| 225 | + else: |
| 226 | + suggestion = "" |
| 227 | + raise FieldDoesNotExist( |
| 228 | + f"Unsupported lookup '{name}' for " |
| 229 | + f"EmbeddedModelArrayField of '{output_field.__class__.__name__}'" |
| 230 | + f"{suggestion}" |
| 231 | + ) |
| 232 | + |
| 233 | + def as_mql(self, compiler, connection): |
| 234 | + inner_lhs_mql = self._lhs.as_mql(compiler, connection) |
| 235 | + lhs_mql = process_lhs(self, compiler, connection) |
| 236 | + return { |
| 237 | + "$ifNull": [ |
| 238 | + { |
| 239 | + "$map": { |
| 240 | + "input": lhs_mql, |
| 241 | + "as": "item", |
| 242 | + "in": inner_lhs_mql, |
| 243 | + } |
| 244 | + }, |
| 245 | + [], |
| 246 | + ] |
| 247 | + } |
| 248 | + |
| 249 | + @property |
| 250 | + def output_field(self): |
| 251 | + return _EmbeddedModelArrayOutputField(self._lhs.output_field) |
| 252 | + |
| 253 | + |
| 254 | +class KeyTransformFactory: |
| 255 | + def __init__(self, key_name, base_field): |
| 256 | + self.key_name = key_name |
| 257 | + self.base_field = base_field |
| 258 | + |
| 259 | + def __call__(self, *args, **kwargs): |
| 260 | + return KeyTransform(self.key_name, self.base_field, *args, **kwargs) |
0 commit comments