1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #34975 -- Complemented rhs filtering aggregations for __in lookup.

While this isn't a regression it's clear that similar logic should be
applied when dealing with lists of expressions passed as a lookup value.
This commit is contained in:
Simon Charette 2023-11-17 20:40:31 -05:00 committed by Mariusz Felisiak
parent 7530cf3900
commit 15cb3c262a
3 changed files with 15 additions and 1 deletions

View File

@ -471,6 +471,14 @@ class IntegerLessThanOrEqual(IntegerFieldOverflow, LessThanOrEqual):
class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup): class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
lookup_name = "in" lookup_name = "in"
def get_refs(self):
refs = super().get_refs()
if self.rhs_is_direct_value():
for rhs in self.rhs:
if get_rhs_refs := getattr(rhs, "get_refs", None):
refs |= get_rhs_refs()
return refs
def get_prep_lookup(self): def get_prep_lookup(self):
from django.db.models.sql.query import Query # avoid circular import from django.db.models.sql.query import Query # avoid circular import

View File

@ -1268,7 +1268,7 @@ class Query(BaseExpression):
# The items of the iterable may be expressions and therefore need # The items of the iterable may be expressions and therefore need
# to be resolved independently. # to be resolved independently.
values = ( values = (
self.resolve_lookup_value(sub_value, can_reuse, allow_joins) self.resolve_lookup_value(sub_value, can_reuse, allow_joins, summarize)
for sub_value in value for sub_value in value
) )
type_ = type(value) type_ = type(value)

View File

@ -2315,3 +2315,9 @@ class AggregateAnnotationPruningTests(TestCase):
max_book_author=Max("book__authors"), max_book_author=Max("book__authors"),
).aggregate(count=Count("id", filter=Q(id=F("max_book_author")))) ).aggregate(count=Count("id", filter=Q(id=F("max_book_author"))))
self.assertEqual(aggregates, {"count": 1}) self.assertEqual(aggregates, {"count": 1})
def test_aggregate_reference_lookup_rhs_iter(self):
aggregates = Author.objects.annotate(
max_book_author=Max("book__authors"),
).aggregate(count=Count("id", filter=Q(id__in=[F("max_book_author"), 0])))
self.assertEqual(aggregates, {"count": 1})