Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions django/contrib/gis/db/backends/oracle/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def django_test_skips(self):
skips.update(
{
"Oracle 23ai unpacks 1-element geometry collections.": {
"gis_tests.geogapp.tests.GeographyTest."
"test05_geography_layermapping",
"gis_tests.layermap.tests.LayerMapTest."
"test_layermap_unique_multigeometry_fk",
"gis_tests.layermap.tests.LayerMapTest."
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/sql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def find_ordering_name(
"""
name, order = get_order_dir(name, default_order)
descending = order == "DESC"
pieces = name.split(LOOKUP_SEP)
pieces = self.query.get_names_to_join(name)
(
field,
targets,
Expand Down
13 changes: 11 additions & 2 deletions django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,13 @@ def try_transform(self, lhs, name, lookups=None):
"permitted%s" % (unsupported_lookup, output_field.__name__, suggestion)
)

def get_names_to_join(self, expr):
"""
Helper method for the resolution of expressions that could either be a
FilteredRelation alias or a field lookup.
"""
return [expr] if expr in self._filtered_relations else expr.split(LOOKUP_SEP)

def build_filter(
self,
filter_expr,
Expand Down Expand Up @@ -2276,10 +2283,11 @@ def add_fields(self, field_names, allow_m2m=True):
try:
cols = []
for name in field_names:
names_to_join = self.get_names_to_join(name)
# Join promotion note - we must not remove any rows here, so
# if there is no existing joins, use outer join.
join_info = self.setup_joins(
name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m
names_to_join, opts, alias, allow_many=allow_m2m
)
targets, final_alias, joins = self.trim_joins(
join_info.targets,
Expand Down Expand Up @@ -2343,9 +2351,10 @@ def add_ordering(self, *ordering):
continue
if self.extra and item in self.extra:
continue
names_to_join = self.get_names_to_join(item)
# names_to_path() validates the lookup. A descriptive
# FieldError will be raise if it's not.
self.names_to_path(item.split(LOOKUP_SEP), self.model._meta)
self.names_to_path(names_to_join, self.model._meta)
elif not hasattr(item, "resolve_expression"):
errors.append(item)
if getattr(item, "contains_aggregate", False):
Expand Down
23 changes: 23 additions & 0 deletions tests/filtered_relation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,29 @@ def test_values(self):
],
)

def test_lookup_sep_in_alias_values(self):
alias = "editor__name"
queryset = Book.objects.annotate(
**{alias: FilteredRelation("editor", condition=Q(editor__isnull=False))}
).filter(title="The book by Alice")
self.assertSequenceEqual(queryset.values(alias), [{alias: self.editor_a.pk}])
self.assertSequenceEqual(queryset.values_list(alias), [(self.editor_a.pk,)])

def test_lookup_sep_in_alias_order_by(self):
alias = "editor__name"
editor_d = Editor.objects.create(name="d")
editor_c = Editor.objects.create(name="c")
book5 = Book.objects.create(
title="Poems from outer space", editor=editor_d, author=self.author1
)
book6 = Book.objects.create(
title="Stories from outer space", editor=editor_c, author=self.author1
)
queryset = Book.objects.annotate(
**{alias: FilteredRelation("editor", condition=Q(editor__isnull=False))}
).filter(title__contains="outer space")
self.assertSequenceEqual(queryset.order_by(alias), [book5, book6])

def test_extra(self):
self.assertSequenceEqual(
Author.objects.annotate(
Expand Down
Loading