[Answered ]-Prefetch by related_name in children foreign key django

1๐Ÿ‘

โœ…

.prefetch_related(โ€ฆ) [Django-doc] does not fetches the ModelBs in the same query, but in a second query where it fetches all the related ModelBs for the selected (filtered) ModelAs in bulk, this in contract to fetching it per ModelA object which would be the usual behavior.

For a DetailView [Django-doc], it will thus not make any improvement. In a DetailView you render a single item, and fetching the related ModelBs will, regardless whether it is done through .prefetch_related(โ€ฆ) or by accessing object.model_a_related.all() make one extra query.

Leave a comment