Django prefetch_related children of children

According to the docs you can do this:

Restaurant.objects.prefetch_related('pizzas__toppings')

This will prefetch all pizzas belonging to restaurants, and all toppings belonging to those pizzas. This will result in a total of 3 database queries - one for the restaurants, one for the pizzas, and one for the toppings.

or you can use the Prefetch object to further control the prefetch operation.

from django.db.models import Prefetch

Restaurant.objects.prefetch_related(Prefetch('pizzas__toppings', queryset=Toppings.objects.order_by('name')))