i'm trying to prefetch related from parent model to chidren throught the related name, However the queryset in the template still hits the DB in PostgreSQL, my modelB modelC and ModelD all point towards modelA and when I overwrite the generic class based view queryset function it still doesnt affect the size of the query?? any clues ?
*MODEL
class ModelA(models.Model):
title = models.Charfield(max_lenght=200, null=True, Blank=True)
class ModelB(models.Model):
model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name="model_a_related")
*VIEW
class ModelAView(DetailView):
model = ModelA
def get_queryset(self):
return super().get_queryset().prefetch_related('model_a_related')
.prefetch_related
does not fetches theModelB
s in the same query, but in a second query. For aDetailView
, it will not make an improvement:.prefetch_related
is useful if you render a set ofModelA
s, since then you prevent making a query perModelA
object to fetch the relatedModelB
s. – Willem Van Onsem