|
4 | 4 | from django.core import checks, exceptions
|
5 | 5 | from django.db.models import DecimalField, Field, Func, IntegerField, Transform, Value
|
6 | 6 | from django.db.models.fields.mixins import CheckFieldDefaultMixin
|
7 |
| -from django.db.models.lookups import In |
| 7 | +from django.db.models.lookups import In, Lookup |
8 | 8 | from django.utils.translation import gettext_lazy as _
|
9 | 9 |
|
10 | 10 | from django_mongodb.forms import SimpleArrayField
|
11 | 11 |
|
12 |
| -from ..query_utils import process_lhs |
| 12 | +from ..query_utils import process_lhs, process_rhs |
13 | 13 | from ..utils import prefix_validation_error
|
14 | 14 |
|
15 | 15 | __all__ = ["ArrayField"]
|
@@ -266,6 +266,27 @@ def _rhs_not_none_values(self, rhs):
|
266 | 266 | yield True
|
267 | 267 |
|
268 | 268 |
|
| 269 | +@ArrayField.register_lookup |
| 270 | +class ArrayContains(Lookup): # ArrayRHSMixin, lookups.DataContains): |
| 271 | + lookup_name = "contains" |
| 272 | + |
| 273 | + def as_mql(self, compiler, connection): |
| 274 | + lhs_mql = process_lhs(self, compiler, connection) |
| 275 | + value = process_rhs(self, compiler, connection) |
| 276 | + return { |
| 277 | + "$gt": [ |
| 278 | + { |
| 279 | + "$cond": { |
| 280 | + "if": {"$eq": [lhs_mql, None]}, |
| 281 | + "then": None, |
| 282 | + "else": {"$size": {"$setIntersection": [lhs_mql, value]}}, |
| 283 | + } |
| 284 | + }, |
| 285 | + 0, |
| 286 | + ] |
| 287 | + } |
| 288 | + |
| 289 | + |
269 | 290 | # @ArrayField.register_lookup
|
270 | 291 | # class ArrayExact(ArrayRHSMixin, Exact):
|
271 | 292 | # pass
|
|
0 commit comments