[Django]-Prefetch nested through table

3👍

You can do a nested prefetch to get the related Orders and from there the related OrderLines:

customers = Customer.objects.prefetch_related(
    Prefetch(
        'order_customer',
        queryset=Order.objects.prefetch_related(Prefetch(
            'orderline_order',
            queryset=OrderLine.objects.filter(**query)
        ))
    )
)

Leave a comment