Django nested query in From clause
Is there any way to construct a query like the following using Django ORM?
SELECT * from ( SELECT r1 from table_name ) temp;
You can use Subquery as the docs says.
Here is an example:
from django.db.models import Subquery
Table.objects.filter(id__in=Subquery(Table2.objects.filter(...).values_list("table_id", flat=True)))