Django operator, which returns a queryset that follows and caches foreign key relationships, in order to avoid hitting the database in future calls that require use of foreign keys.
Questions tagged [django-select-related]
147 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
164
votes
1 answer
How to use 'select_related' with get_object_or_404?
Is there any way of using get_object_or_404 and select_related together or any other way to achieve the result of using these two together(except from putting it in try/except)??

Neo
- 5,070
- 10
- 46
- 65
78
votes
3 answers
Django Query Related Field Count
I've got an app where users create pages. I want to run a simple DB query that returns how many users have created more than 2 pages.
This is essentially what I want to do, but of course it's not the right…

Brenden
- 8,264
- 14
- 48
- 78
52
votes
3 answers
Selecting specific fields using select_related in Django
I have two models Article and Blog related using a foreign key. I want to select only blog name while extracting the article.
articles = Articles.objects.all().select_related('blog__name')
The query generated shows that it selected all the fields…

anonDuck
- 1,337
- 1
- 9
- 15
36
votes
3 answers
A left outer reverse select_related in Django?
Imagine the following model:
class Parent(Model):
...
class Child(Model)
father = ForeignKey(Parent)
...
Some parents have children, others do not (they're not parents in the real meaning, it's just a fictional name).
I would like to…

augustomen
- 8,977
- 3
- 43
- 63
33
votes
2 answers
Can I use select_related() with ManyToManyField on Django Models?
I have :
class Award(models.Model) :
name = models.CharField(max_length=100, db_index=True)
class Alias(models.Model) :
awards = models.ManyToManyField('Award', through='Achiever')
class Achiever(models.Model):
award =…

Paul Tarjan
- 48,968
- 59
- 172
- 213
23
votes
4 answers
Optimizing database queries in Django REST framework
I have the following models:
class User(models.Model):
name = models.Charfield()
email = models.EmailField()
class Friendship(models.Model):
from_friend = models.ForeignKey(User)
to_friend = models.ForeignKey(User)
And those models…

dowjones123
- 3,695
- 5
- 40
- 83
17
votes
4 answers
django select_related for multiple foreign keys
How does select_related work with a model which has multiple foreign keys? Does it just choose the first one?
class Model:
fkey1, fkey2, fkey3...
The documentation doesn't say anything about this, at least not in where the method is specified.
NOTE:…

dtc
- 1,774
- 2
- 21
- 44
16
votes
2 answers
Django select_related on chained foreign keys
I read the documentation and all the related questions here but I haven't properly understood how select_related behaves on chained/multiple foreign keys.
Assume we have the following models:
class RecordLabel(models.Model):
title =…

Fotis Sk
- 313
- 2
- 10
15
votes
3 answers
Is there a way to check whether a related object is already fetched?
I would like to be able to check if a related object has already been fetched by using either select_related or prefetch_related, so that I can serialize the data accordingly. Here is an example:
class Address(models.Model):
street =…

basilikum
- 10,378
- 5
- 45
- 58
12
votes
3 answers
Update Django from 1.6 to 1.8: Invalid field name(s) given in select_related
I update project from Django 1.6.7 to 1.8.7 and I have got following exception with Django 1.8, although with Django 1.6 it code was right:
In[2]: from apps.route import models
In[3]: models.Trace.objects.select_related("trace_points")
Out[3]:…

Peter
- 1,223
- 3
- 11
- 22
10
votes
1 answer
In Django, Can I `defer()` fields in an object that's being queried by `select_related()`
In my Django app I want to use select_related() on a QuerySet to "follow" a ForeignKey field, but I only need to access a few of the fields on the "followed" model instance. Can I use the defer() method somehow with my "followed" field.
e.g., if I…

Chris W.
- 37,583
- 36
- 99
- 136
10
votes
1 answer
DJANGO: How to sort objects based on attribute of a related model?
I have a User model and UserProfile model. In the User model I'd like to order my query so that its in alphabetical order by last_name. Then I'd like to order it by the User_profiles "title" attribute (Manager, Executive, Accountant etc).…

thedeepfield
- 6,138
- 25
- 72
- 107
9
votes
1 answer
Non-relational field given in select_related: ' '. Choices are: (none)
I have two models from different apps:
class Measure(models.Model):
date = models.DateTimeField(default="2018-01-23 15:55")
average = models.FloatField(default=0)
class Sensor(models.Model):
measure=models.ForeignKey(Measure,…

Alvaro
- 1,430
- 2
- 23
- 41
9
votes
3 answers
Django proper use of select_related or prefetch_related on a ForeignKey
I'm trying to figure out how to use select_related or prefetch_related to optimize a query from the other end of a foreign key. For example:
Say I have some models such as the following:
class Product(models.Model):
name =…

Joe J
- 9,985
- 16
- 68
- 100