I have the following models
class Film(models.Model):
crew = models.ManyToManyField(\'Person\', through=\'Role\', blank=True)
class Role(models.Model):
You can construct your queryset like this:
from django.db.models import Prefetch
def get_queryset(self):
return super(FilmDetail, self).get_queryset().prefetch_related(
Prefetch(
'crew',
queryset=Role.objects.select_related(
'person',
'person_role',
),
),
)
Only Film->Role is a backwards relation loadable with prefetch_related
. Role->RoleType and Role->Person are forwards relations that you load with select_related
.