diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 79499b2854..171e72cfb8 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1760,7 +1760,7 @@ class Query(BaseExpression): ) """ # Generate the inner query. - query = Query(self.model) + query = self.__class__(self.model) query._filtered_relations = self._filtered_relations filter_lhs, filter_rhs = filter_expr if isinstance(filter_rhs, OuterRef):