Skip to content

Commit af2710d

Browse files
committed
Implement ColPairs.as_mql() to fix regression in some prefetch_related()
Adapt for Django 5.2 which introduced ColPairs.
1 parent daaf7a4 commit af2710d

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

django_mongodb_backend/expressions.py

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.db.models.expressions import (
99
Case,
1010
Col,
11+
ColPairs,
1112
CombinedExpression,
1213
Exists,
1314
ExpressionList,
@@ -74,6 +75,13 @@ def col(self, compiler, connection): # noqa: ARG001
7475
return f"${prefix}{self.target.column}"
7576

7677

78+
def col_pairs(self, compiler, connection):
79+
cols = self.get_cols()
80+
if len(cols) > 1:
81+
raise NotSupportedError("ColPairs is not supported.")
82+
return cols[0].as_mql(compiler, connection)
83+
84+
7785
def combined_expression(self, compiler, connection):
7886
expressions = [
7987
self.lhs.as_mql(compiler, connection),
@@ -211,6 +219,7 @@ def value(self, compiler, connection): # noqa: ARG001
211219
def register_expressions():
212220
Case.as_mql = case
213221
Col.as_mql = col
222+
ColPairs.as_mql = col_pairs
214223
CombinedExpression.as_mql = combined_expression
215224
Exists.as_mql = exists
216225
ExpressionList.as_mql = expression_list

django_mongodb_backend/features.py

-9
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9090
"model_fields_.test_arrayfield.QueryingTests.test_contained_by_subquery",
9191
# JSONArray not implemented.
9292
"db_functions.json.test_json_array.JSONArrayTests",
93-
# Some usage of prefetch_related() raises "ColPairs is not supported."
94-
"known_related_objects.tests.ExistingRelatedInstancesTests.test_one_to_one_multi_prefetch_related",
95-
"known_related_objects.tests.ExistingRelatedInstancesTests.test_one_to_one_prefetch_related",
96-
"prefetch_related.tests.DeprecationTests.test_prefetch_one_level_fallback",
97-
"prefetch_related.tests.MultiDbTests.test_using_is_honored_fkey",
98-
"prefetch_related.tests.MultiDbTests.test_using_is_honored_inheritance",
99-
"prefetch_related.tests.NestedPrefetchTests.test_nested_prefetch_is_not_overwritten_by_related_object",
100-
"prefetch_related.tests.NullableTest.test_prefetch_nullable",
101-
"prefetch_related.tests.Ticket19607Tests.test_bug",
10293
# Value.as_mql() doesn't call output_field.get_db_prep_save():
10394
# https://github.com/mongodb/django-mongodb-backend/issues/282
10495
"model_fields.test_jsonfield.TestSaveLoad.test_bulk_update_custom_get_prep_value",

django_mongodb_backend/lookups.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.db import NotSupportedError
2-
from django.db.models.expressions import ColPairs
32
from django.db.models.fields.related_lookups import In, RelatedIn
43
from django.db.models.lookups import (
54
BuiltinLookup,
@@ -35,8 +34,6 @@ def field_resolve_expression_parameter(self, compiler, connection, sql, param):
3534

3635

3736
def in_(self, compiler, connection):
38-
if isinstance(self.lhs, ColPairs):
39-
raise NotImplementedError("ColPairs is not supported.")
4037
db_rhs = getattr(self.rhs, "_db", None)
4138
if db_rhs is not None and db_rhs != connection.alias:
4239
raise ValueError(

0 commit comments

Comments
 (0)