in Education by
What is the difference between select_related and prefetch_related?

1 Answer

0 votes
by

Though both the functions are used to fetch the related fields on a model but their functioning is bit different from each other. In simple words, select_related uses a foreign key relationship, i.e. using join on the query itself while on the prefetch_related there is a separate lookup and the joining on the python side. Let’s try to illustrate this via an example:

from django.db import models
class Country(models.Model):
    country_name = models.CharField(max_length=5)
class State(models.Model):
    state_name = models.CharField(max_length=5)
    country = model.ForeignKey(Country)
>> states = State.objects.select_related(‘country’).all()
>> for state in states:
…   print(state.state_name)  
```Query Executed
SELECT state_id, state_name, country_name FROM State INNER JOIN Country ON (State.country_id = Country.id)
```
>> country = Country.objects.prefetch_related(‘state’).get(id=1)
>> for state in country.state.all():
…   print(state.state_name)
```Query Executed
SELECT id, country_name FROM country WHERE id=1;
SELECT state_id, state_name WHERE State WHERE country_id IN (1);
```

Related questions

0 votes
    What is the use of the post_delete signal in Django? 1. Sent before a model’s save() method is called 2. ... called 4. Sent after a model’s delete() method is called...
asked Jul 4, 2021 in Education by JackTerrance
0 votes
    Which is the default port for the Django development server? 1. 8080 2. 8081 3. 8000 4. 9000...
asked Jul 4, 2021 in Education by JackTerrance
0 votes
    What is mixin in Django?...
asked Jul 2, 2021 in Education by JackTerrance
0 votes
    What are the difference between Task and Thread in .NET?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    What's the difference between SDK and Runtime in .NET Core?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    What is the difference between .NET Core and Mono?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    The main difference between SNMPv3 and SNMPv2 is _______. A. Enhanced security B. Integration C. Management D. Integration...
asked Feb 14, 2023 in Technology by JackTerrance
0 votes
    What is the difference between an abstract class and an interface?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is the difference between a constructor and a method?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is the difference between method overloading and method overriding?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is the difference between Maven and ANT?...
asked Feb 5, 2023 in Technology by JackTerrance
0 votes
    Can you explain the difference between Data Warehousing and Business Intelligence?...
asked Feb 4, 2023 in Technology by JackTerrance
0 votes
    What is the difference between AI, Machine Learning and Deep Learning?...
asked Jan 17, 2023 in Education by JackTerrance
0 votes
    Which command shows the difference between the working directory and the index or staging area? A. git status B. git diff...
asked Dec 17, 2022 in Technology by JackTerrance
0 votes
    Difference between province and state Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
...