[Answered ]-Prefetch_related() ForeignKey reverse lookup in Django?

2👍

prefetch_related just adds the SQL query to get the suppliers so that when you do company.suppliers.all() you don’t incur another SQL hit on top of Company.objects.all(). You can access the suppliers normally thereafter:

companies = Company.objects.all().prefetch_related('suppliers')

for company in companies:
    # Does not require a new SQL query
    suppliers = company.suppliers.all()

Leave a comment