1👍
✅
Probably:
email_reports = EmailReport.objects.prefetch_related('payment_report__shop__users').filter(pk__in=ids)
(That are ids
here? Users ids or EmailReport ids?)
This shoud be minimal amount of queries. Just remember that you can’t do further filtering or it will break the prefetch.
Then there are few ways to get the users. The most simple will probably be: (but can be unefficient)
users_from_query = []
for email_report in email_reports:
for pr in email_report.payment_report.all()
users_from_query.extend(pr.shop.users.all())
Source:stackexchange.com