Django - Get Foreign key objects in single query? Django - Get Foreign key objects in single query? django django

Django - Get Foreign key objects in single query?


I think you are looking for select_related().

This line:

actors = Actor.objects.filter(programme = programme_id)

should look like this:

actors = Actor.objects.select_related().filter(programme = programme_id)

Unfortunately as emphasized here: Get foreign key objects in a single query you will only be able to retrieve actors that way as select_related only works on objects having ForeignKeys and not vice versa.


You query Programme and assign to programme, but you never use the result anywhere. Just remove that line.