Hello everyone, I have the following function to get a re-registration with all the profiles who participate in it and their proportions. I think because I use the annotate I get all the GarageOwnerHistory of this profile and am forced to filter profiles using the garage specified in the registration. If I understand correctly how the prefetch_related works, first the request for re-registration is executed, and then the request for all profiles. Is there any way to get the data from the first query to use it in the prefetch_related or is the way I did the only option ?
def get_reregistration(pk: int)-> ReRegistration:
garage = get_object_or_404(ReRegistration.objects.only('garage'), id=pk).garage
old_owners_proportion_sub = GarageOwnerHistory.objects.filter(id=OuterRef('garageownerhistory__id')).values('proportion')
reregistration = get_object_or_404(ReRegistration.objects.prefetch_related(
Prefetch('old_owners', queryset=Profile.objects
.filter(Q(garageownerhistory__garage=garage))
.annotate(proportion=Subquery(old_owners_proportion_sub)))), id=pk)
return reregistration