Skip to content

Commit fde2022

Browse files
committed
fix "AttributeError: pattern_ops" query crash
1 parent 346549e commit fde2022

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

django_mongodb/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ class DatabaseWrapper(BaseDatabaseWrapper):
7272
"istartswith": "LIKE UPPER(%s)",
7373
"iendswith": "LIKE UPPER(%s)",
7474
}
75+
# As with `operators`, these patterns are used to generate SQL before MQL.
76+
pattern_esc = "%%"
77+
pattern_ops = {
78+
"contains": "LIKE '%%' || {} || '%%'",
79+
"icontains": "LIKE '%%' || UPPER({}) || '%%'",
80+
"startswith": "LIKE {} || '%%'",
81+
"istartswith": "LIKE UPPER({}) || '%%'",
82+
"endswith": "LIKE '%%' || {}",
83+
"iendswith": "LIKE '%%' || UPPER({})",
84+
}
7585
mongo_operators = {
7686
"exact": lambda a, b: {"$eq": [a, b]},
7787
"gt": lambda a, b: {"$gt": [a, b]},

django_mongodb/features.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6969
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
7070
# 'Ref' object has no attribute 'as_mql'.
7171
"expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation",
72-
# AttributeError: pattern_ops
73-
"expressions.tests.BasicExpressionsTests.test_annotation_with_nested_outerref",
74-
"expressions.tests.BasicExpressionsTests.test_nested_outerref_with_function",
7572
}
7673
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
7774
_django_test_expected_failures_bitwise = {
@@ -290,9 +287,11 @@ def django_test_expected_failures(self):
290287
"annotations.tests.NonAggregateAnnotationTestCase.test_empty_queryset_annotation",
291288
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_outerref",
292289
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_subquery_with_parameters",
290+
"expressions.tests.BasicExpressionsTests.test_annotation_with_nested_outerref",
293291
"expressions.tests.BasicExpressionsTests.test_annotation_with_outerref",
294292
"expressions.tests.BasicExpressionsTests.test_annotations_within_subquery",
295293
"expressions.tests.BasicExpressionsTests.test_in_subquery",
294+
"expressions.tests.BasicExpressionsTests.test_nested_outerref_with_function",
296295
"expressions.tests.BasicExpressionsTests.test_nested_subquery",
297296
"expressions.tests.BasicExpressionsTests.test_nested_subquery_join_outer_ref",
298297
"expressions.tests.BasicExpressionsTests.test_nested_subquery_outer_ref_2",

0 commit comments

Comments
 (0)