[Django]-Django – Get Foreign key objects in single query?

14👍

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

32👍

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.

Leave a comment