From 2d6c0b540bcb187f3d80842f093626bf3978eb91 Mon Sep 17 00:00:00 2001 From: Emanuel Lupi Date: Tue, 18 Mar 2025 00:47:16 -0300 Subject: [PATCH 1/2] Add support for JSONArray function --- django_mongodb_backend/features.py | 2 -- django_mongodb_backend/functions.py | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/django_mongodb_backend/features.py b/django_mongodb_backend/features.py index bb709b1f1..4fdef5e1f 100644 --- a/django_mongodb_backend/features.py +++ b/django_mongodb_backend/features.py @@ -87,8 +87,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): # of $setIsSubset must be arrays. Second argument is of type: null" # https://jira.mongodb.org/browse/SERVER-99186 "model_fields_.test_arrayfield.QueryingTests.test_contained_by_subquery", - # JSONArray not implemented. - "db_functions.json.test_json_array.JSONArrayTests", # Some usage of prefetch_related() raises "ColPairs is not supported." "known_related_objects.tests.ExistingRelatedInstancesTests.test_one_to_one_multi_prefetch_related", "known_related_objects.tests.ExistingRelatedInstancesTests.test_one_to_one_prefetch_related", diff --git a/django_mongodb_backend/functions.py b/django_mongodb_backend/functions.py index 750a548f8..3f41828b6 100644 --- a/django_mongodb_backend/functions.py +++ b/django_mongodb_backend/functions.py @@ -1,5 +1,6 @@ from django.db import NotSupportedError from django.db.models.expressions import Func +from django.db.models.functions import JSONArray from django.db.models.functions.comparison import Cast, Coalesce, Greatest, Least, NullIf from django.db.models.functions.datetime import ( Extract, @@ -106,6 +107,10 @@ def func(self, compiler, connection): return {f"${operator}": lhs_mql} +def json_array(self, compiler, connection, **extra_context): # noqa: ARG001 + return process_lhs(self, compiler, connection) + + def left(self, compiler, connection): return self.get_substr().as_mql(compiler, connection) @@ -238,6 +243,7 @@ def register_functions(): Cot.as_mql = cot Extract.as_mql = extract Func.as_mql = func + JSONArray.as_mql = json_array Left.as_mql = left Length.as_mql = length Log.as_mql = log From d0a138e748b1b8a400dd24b854a5a245eab913eb Mon Sep 17 00:00:00 2001 From: Emanuel Lupi Date: Tue, 18 Mar 2025 02:02:07 -0300 Subject: [PATCH 2/2] Remove wrapping function --- django_mongodb_backend/functions.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/django_mongodb_backend/functions.py b/django_mongodb_backend/functions.py index 3f41828b6..191ae9cae 100644 --- a/django_mongodb_backend/functions.py +++ b/django_mongodb_backend/functions.py @@ -107,10 +107,6 @@ def func(self, compiler, connection): return {f"${operator}": lhs_mql} -def json_array(self, compiler, connection, **extra_context): # noqa: ARG001 - return process_lhs(self, compiler, connection) - - def left(self, compiler, connection): return self.get_substr().as_mql(compiler, connection) @@ -243,7 +239,7 @@ def register_functions(): Cot.as_mql = cot Extract.as_mql = extract Func.as_mql = func - JSONArray.as_mql = json_array + JSONArray.as_mql = process_lhs Left.as_mql = left Length.as_mql = length Log.as_mql = log