pythondjangodjango-queryset

how to subquery in queryset in django?


how can i have a subquery in django's queryset? for example if i have:

select name, age from person, employee where person.id = employee.id and
employee.id in (select id from employee where employee.company = 'Private')

this is what i have done yet.

Person.objects.value('name', 'age')
Employee.objects.filter(company='Private')

but it not working because it returns two output...


Solution

  • ids = Employee.objects.filter(company='Private').values_list('id', flat=True)
    Person.objects.filter(id__in=ids).values('name', 'age')