I am trying to understand the use of prefetch_related and select_related for optimization. I learned somewhere in the blog that one of the where to use prefetch and select is, prefetch_related is used for reverse relation and select_related for the forward relation. In my case there is Resume model and Education Model. Education model has FK of resume with the related_name for reverse relation instead of writing additional _set when querying. I need to list all the education of an requested user with the requested user resume. I could do this without those optimization techniques as follow
education_instance = Resume.objects.get(applicant=request.user).educations.all()
When I tried to use the following instead, I get the error I stated in the title
education_instance = Resume.objects.filter(applicant=request.user).prefetch_related('educations')
Here is my model
class Resume(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, blank=False, null=False, help_text="Full Name")
class Education(models.Model):
resume = models.ForeignKey(Resume, related_name='educations')
name = models.CharField(max_length=100, blank=False, null=False, help_text="Name of an institution")
Can anyone make me clear on select_related and prefetch_related with simple layman terms ? I could not understand the issue I am getting