[Answered ]-Django Select_related(): from another model has a ForginKey To the model that will execute Select_related

1👍

This should works:

consultat = Consultation.objects.prefetch_related('reply').filter(user=request.user)

Since it is a one-to-many relation, you’ll need a prefetch_related instead (see this link). And you also need to specify which field(s) you want to prefetch as well.

prefetch_related, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related,

Leave a comment