Forwarded from PyNotes
#Django_ORM #filters
Subquery
Django allows using SQL subqueries. Letβs start with something simple, We have a UserParent model which has OnetoOne relation with auth user. We will find all the UserParent which have a UserParent.
from django.db.models import Subquery
users = User.objects.all()
UserParent.objects.filter(user_id__in=Subquery(users.values('id')))
#source
https://books.agiliq.com/projects/django-orm-cookbook/en/latest/subquery.html
Subquery
Django allows using SQL subqueries. Letβs start with something simple, We have a UserParent model which has OnetoOne relation with auth user. We will find all the UserParent which have a UserParent.
from django.db.models import Subquery
users = User.objects.all()
UserParent.objects.filter(user_id__in=Subquery(users.values('id')))
#source
https://books.agiliq.com/projects/django-orm-cookbook/en/latest/subquery.html