Skip to content

Commit 18c9da9

Browse files
committed
fix "AttributeError: pattern_ops" query crash
1 parent ae6cfd6 commit 18c9da9

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
@@ -104,9 +104,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
104104
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
105105
# 'Ref' object has no attribute 'as_mql'.
106106
"expressions.tests.BasicExpressionsTests.test_aggregate_subquery_annotation",
107-
# AttributeError: pattern_ops
108-
"expressions.tests.BasicExpressionsTests.test_annotation_with_nested_outerref",
109-
"expressions.tests.BasicExpressionsTests.test_nested_outerref_with_function",
110107
}
111108
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
112109
_django_test_expected_failures_bitwise = {
@@ -329,9 +326,11 @@ def django_test_expected_failures(self):
329326
"annotations.tests.NonAggregateAnnotationTestCase.test_empty_queryset_annotation",
330327
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_outerref",
331328
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_subquery_with_parameters",
329+
"expressions.tests.BasicExpressionsTests.test_annotation_with_nested_outerref",
332330
"expressions.tests.BasicExpressionsTests.test_annotation_with_outerref",
333331
"expressions.tests.BasicExpressionsTests.test_annotations_within_subquery",
334332
"expressions.tests.BasicExpressionsTests.test_in_subquery",
333+
"expressions.tests.BasicExpressionsTests.test_nested_outerref_with_function",
335334
"expressions.tests.BasicExpressionsTests.test_nested_subquery",
336335
"expressions.tests.BasicExpressionsTests.test_nested_subquery_join_outer_ref",
337336
"expressions.tests.BasicExpressionsTests.test_nested_subquery_outer_ref_2",

0 commit comments

Comments
 (0)