Skip to content

Commit d57986b

Browse files
committed
make query generation reference fields with a dollar sign by default
1 parent 3789b9f commit d57986b

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

django_mongodb/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def col(self, compiler, connection): # noqa: ARG001
5-
return self.target.column
5+
return f"${self.target.column}"
66

77

88
def register_expressions():

django_mongodb/functions.py

Lines changed: 0 additions & 3 deletions
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 Col
32
from django.db.models.functions.datetime import Extract
43

54
from .query_utils import process_lhs
@@ -15,8 +14,6 @@ def extract(self, compiler, connection):
1514
operator = "$year"
1615
else:
1716
raise NotSupportedError("%s is not supported." % self.__class__.__name__)
18-
if isinstance(self.lhs, Col):
19-
lhs_mql = f"${lhs_mql}"
2017
return {operator: lhs_mql}
2118

2219

django_mongodb/lookups.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from django.db import NotSupportedError
2-
from django.db.models.expressions import Col
32
from django.db.models.fields.related_lookups import In, MultiColSource, RelatedIn
43
from django.db.models.lookups import BuiltinLookup, Exact, IsNull, UUIDTextMixin
54

65
from .query_utils import process_lhs, process_rhs
76

87

98
def builtin_lookup(self, compiler, connection):
10-
lhs_mql = process_lhs(self, compiler, connection)
9+
lhs_mql = process_lhs(self, compiler, connection, bare_column_ref=True)
1110
value = process_rhs(self, compiler, connection)
1211
rhs_mql = connection.operators[self.lookup_name](value)
1312
return {lhs_mql: rhs_mql}
@@ -16,8 +15,6 @@ def builtin_lookup(self, compiler, connection):
1615
def exact(self, compiler, connection):
1716
lhs_mql = process_lhs(self, compiler, connection)
1817
value = process_rhs(self, compiler, connection)
19-
if isinstance(self.lhs, Col):
20-
lhs_mql = f"${lhs_mql}"
2118
return {"$expr": {"$eq": [lhs_mql, value]}}
2219

2320

@@ -30,7 +27,7 @@ def in_(self, compiler, connection):
3027
def is_null(self, compiler, connection):
3128
if not isinstance(self.rhs, bool):
3229
raise ValueError("The QuerySet value for an isnull lookup must be True or False.")
33-
lhs_mql = process_lhs(self, compiler, connection)
30+
lhs_mql = process_lhs(self, compiler, connection, bare_column_ref=True)
3431
rhs_mql = connection.operators["isnull"](self.rhs)
3532
return {lhs_mql: rhs_mql}
3633

django_mongodb/query_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ def is_direct_value(node):
55
return not hasattr(node, "as_sql")
66

77

8-
def process_lhs(node, compiler, connection):
8+
def process_lhs(node, compiler, connection, bare_column_ref=False):
99
if is_direct_value(node.lhs):
1010
return node
11-
return node.lhs.as_mql(compiler, connection)
11+
mql = node.lhs.as_mql(compiler, connection)
12+
# Remove the unneeded $ from column references.
13+
if bare_column_ref and mql.startswith("$"):
14+
mql = mql[1:]
15+
return mql
1216

1317

1418
def process_rhs(node, compiler, connection):

0 commit comments

Comments
 (0)