Questions tagged [django-prefetch-related]
17 questions
517
votes
6 answers
What's the difference between select_related and prefetch_related in Django ORM?
In Django doc:
select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query.
prefetch_related() does a separate lookup for each relationship, and does the "joining" in Python.
What does…

NeoWang
- 17,361
- 24
- 78
- 126
2
votes
1 answer
Usefulness of calling prefetch_related() without specifying lookups?
I encountered such a queryset in a project :
qs.prefetch_related().other().methods()
Is there a point having such call to prefetch_related() method without specifying any lookup?

vmonteco
- 14,136
- 15
- 55
- 86
1
vote
1 answer
Django only() and values() are not working with prefetch_related()
I want to filter the fields retrieved in a query and get the reverse foreign key using the related_name in a child model using prefetch_related() for the sake of performance optimization when reaching a huge number of database records.
I have the…

DEV
- 65
- 9
1
vote
1 answer
Django: Prefetch on through table with filter
I have the following models:
class Person(models.Model):
name = models.CharField(max_length=255)
class Group(models.Model):
name = models.CharField(max_length=255)
members = models.ManyToManyField(Person, through="Membership")
class…

willowherb
- 837
- 1
- 13
- 21
1
vote
1 answer
Cannot reduce "SELECT" queries with "select_related()" and "prefetch_related()" in one-to-one relationship in Django
I have Person and PersonDetail models in one-to-one relationship as shown below. *I use Django 3.2.16:
class Person(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class…

Super Kai - Kazuya Ito
- 22,221
- 10
- 124
- 129
0
votes
1 answer
Django prefetch related used inside a serializer method making too many queries. How to use prefetch related to reduce queries in following case?
By investigating why some apis are taking too much time to respond I encountered that it's because of the db queries increases as the queryset increases. In Django select related and prefetch related will helps to reduce this problem.
Here I am…

Aswany Mahendran
- 11
- 1
0
votes
1 answer
using select_related with serializer in Django Rest Framework
class User(models.Model):
name = Charfield(...)
age = Integerfield(...)
...
class Article(models.Model):
user = ForeignKey(User, ...)
title = Charfield(...)
content = Charfield(...)
...
class ArticleView(ViewSet):
…

saddeveloper99
- 25
- 4
0
votes
3 answers
How to apply Count on prefetched queryset in django?
To demonstrate my use case, I've devised a similar but simpler setup as follows:
class Student(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Routine(models.Model):
owner = …

Tanvir Ahmed
- 357
- 2
- 15
0
votes
0 answers
Reduce database hit when using django-taggit and mymodel.tags.all in a template
I'm using django-taggit in Django 3.2 and can't figure out how to prevent it from hitting the database for every object in a list view template.
Here's how I'm using it:
class Quest(TagsModelMixin, models.Manager):
# etc.
In a ListView html…

43Tesseracts
- 4,617
- 8
- 48
- 94
0
votes
1 answer
Django REST framework prefetch not working on model with multiple foreign keys to the same target
I'm writing an endpoint to fetch data from the "Term" model in Django REST framework and I'm trying to reduce queries by prefetching data. Specifically there is a model "TermRelation", that saves vector relation scores between individual terms that…

Hanimir
- 3
- 3
0
votes
2 answers
What is the best method to query db in django rest framework
I have models.py
class Category(MPTTModel):
# few fields
class Brand(models.Model):
# few fields
class Attribute(models.Model):
# few fields
class AttributeValue(models.Model):
attributes = models.ForeignKey(Attribute, # other…

Srivatsa
- 94
- 1
- 8
0
votes
1 answer
Nested Serializer for prefetch_related in Django Rest Framework
I'm trying to make a nested serializer with prefetch_related but it doesn't work, here's my code:
models.py
from django.db import models
class Student(models.Model):
phone = models.IntegerField(null=True)
birth_date =…

fudu
- 617
- 1
- 10
- 19
0
votes
1 answer
How to optimize SQL in tree-like comment structure django?
I have posts with tree comments in it. Comments have likes as well. I've managed to optimize SQL queries that usual comments, related to the post itself, don't create new queries, but answers to the other comments still create 3-4 new requests to…

jmur
- 109
- 8
0
votes
0 answers
Django execute couple prefetch_related lookups in a single query
For now I have not found an ability to execute reverse foreign key joins in Django more efficiently that using a prefetch_related a.k.a. joining in python. This method would generate 1 additional query for each reverse lookup and if I have 4-5 of…

sergeyka
- 96
- 1
- 5
0
votes
1 answer
Django reduce amount of queries (M2M relation with through model)
I would like to reduce the amount of similar queries. Here are my models:
class Skill(models.Model):
name = models.TextField()
class Employee(models.Model):
firstname = models.TextField()
skills = models.ManyToManyField(Skill,…

Антон Ласько
- 3
- 2