How do I query foreign-keyed models alongside their parent object?

I have a diary model and a photo model which foreignkeys the diary model.

I want to be able to query all the diary objects and as well get their child photo objects within the same query.
Like Diary.objects.all() should gimme all the diary objects and their different photos.

You can use the prefetch_related method on your queryset to retrieve all Photos related to the Diary models.

1 Like