Skip to content

Commit 243934f

Browse files
committed
Handle refs from foreign collections.
1 parent 01bfffb commit 243934f

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
lines changed

django_mongodb/compiler.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,32 +428,22 @@ def _get_aggregate_expressions(self, expr):
428428
stack.extend(expr.get_source_expressions())
429429

430430
def get_project_fields(self, columns=None, ordering=None):
431-
fields = {}
431+
fields = defaultdict(dict)
432432
for name, expr in columns or []:
433+
collection = expr.alias if isinstance(expr, Col) else None
433434
try:
434-
column = expr.target.column
435-
except AttributeError:
436-
# Generate the MQL for an annotation.
437-
try:
438-
fields[name] = expr.as_mql(self, self.connection)
439-
except EmptyResultSet:
440-
fields[name] = Value(False).as_mql(self, self.connection)
441-
except FullResultSet:
442-
fields[name] = Value(True).as_mql(self, self.connection)
443-
else:
444-
# If name != column, then this is an annotatation referencing
445-
# another column.
446-
fields[name] = 1 if name == column else f"${column}"
447-
if fields:
448-
# Add related fields.
449-
for alias in self.query.alias_map:
450-
if self.query.alias_refcount[alias] and self.collection_name != alias:
451-
fields[alias] = 1
435+
fields[collection][name] = expr.as_mql(self, self.connection)
436+
except EmptyResultSet:
437+
fields[collection][name] = Value(False).as_mql(self, self.connection)
438+
except FullResultSet:
439+
fields[collection][name] = Value(True).as_mql(self, self.connection)
440+
# Unwrap annotations.
441+
fields.update(fields.pop(None, {}))
442+
# Unwrap main collection's fields.
443+
fields.update(fields.pop(self.collection_name, {}))
444+
if fields and ordering:
452445
# Add order_by() fields.
453-
for alias, expression in ordering or []:
454-
nested_entity = alias.split(".", 1)[0] if "." in alias else None
455-
if alias not in fields and nested_entity not in fields:
456-
fields[alias] = expression.as_mql(self, self.connection)
446+
fields.update({alias: expr.as_mql(self, self.connection) for alias, expr in ordering})
457447
return fields
458448

459449
def _get_ordering(self):

django_mongodb/expressions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ def query(self, compiler, connection): # noqa: ARG001
7777

7878

7979
def ref(self, compiler, connection): # noqa: ARG001
80-
return f"${self.refs}"
80+
prefix = (
81+
f"{self.source.alias}."
82+
if isinstance(self.source, Col) and self.source.alias != compiler.collection_name
83+
else ""
84+
)
85+
return f"${prefix}{self.refs}"
8186

8287

8388
def star(self, compiler, connection): # noqa: ARG001

django_mongodb/features.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5050
# Length of null considered zero rather than null.
5151
"db_functions.text.test_length.LengthTests.test_basic",
5252
# Wrong results in queries with multiple tables.
53-
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_reverse_m2m",
54-
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_with_m2m",
55-
"annotations.tests.NonAggregateAnnotationTestCase.test_chaining_annotation_filter_with_m2m",
56-
"annotations.tests.NonAggregateAnnotationTestCase.test_mti_annotations",
57-
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression",
58-
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression_flat",
5953
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
6054
"queries.tests.Queries1Tests.test_order_by_tables",
6155
"queries.tests.TestTicket24605.test_ticket_24605",

0 commit comments

Comments
 (0)