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]
117 questions
69
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…
![](../../users/profiles/636064.webp)
Brenden
- 6,473
- 14
- 39
- 70
37
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…
![](../../users/profiles/5121725.webp)
RA123
- 1,037
- 1
- 7
- 13
33
votes
5 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…
![](../../users/profiles/317971.webp)
augustomen
- 7,153
- 3
- 35
- 58
26
votes
2 answers
Django : select_related with ManyToManyField
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 =…
![](../../users/profiles/90025.webp)
Paul Tarjan
- 44,540
- 54
- 163
- 207
21
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…
![](../../users/profiles/3403972.webp)
dowjones123
- 3,233
- 5
- 34
- 72
14
votes
3 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:…
![](../../users/profiles/921075.webp)
dtc
- 1,608
- 2
- 20
- 40
9
votes
2 answers
Update Django from 1.6 to 1.8: Invalid field name(s) given in select_related
I do update of 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]:…
![](../../users/profiles/2911262.webp)
Peter
- 981
- 2
- 9
- 19
9
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).…
![](../../users/profiles/500401.webp)
thedeepfield
- 5,814
- 23
- 68
- 104
8
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…
![](../../users/profiles/273637.webp)
Chris W.
- 32,518
- 30
- 89
- 121
7
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 =…
![](../../users/profiles/7347318.webp)
Fotis Sk
- 113
- 1
- 9
7
votes
2 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 =…
![](../../users/profiles/1258878.webp)
basilikum
- 9,714
- 4
- 38
- 55
6
votes
1 answer
Django ORM - select_related and order_by with foreign keys
I have a simple music schema: Artist, Release, Track, and Song. The first 3 are all logical constructs while the fourth (Song) is a specific instance of an (Artist, Release, Track) as an mp3, wav, ogg, whatever.
I am having trouble generating an…
![](../../users/profiles/195125.webp)
Rob Crowell
- 1,349
- 2
- 14
- 23
5
votes
2 answers
Django select_related with fields specified breaks over multiple one to one relationships
I'm getting a weird error trying to select_related over multiple OneToOneField relationships, e.g. in the case where the target field is a grandchild subclass. I'd love someone to help me understand what's going on (or confirm that this is a bug in…
![](../../users/profiles/381787.webp)
rfrankel
- 685
- 7
- 20
5
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,…
![](../../users/profiles/5176549.webp)
Alvaro
- 1,146
- 2
- 17
- 33
5
votes
1 answer
How to prefetch a @property with a Django queryset?
I would like to prefetch a model property to a queryset in Django. Is there a way do that ?
Here are the three models:
class Place(models.Model):
name = models.CharField(max_length=200, blank=True)
@property
def bestpicurl(self):
…
![](../../users/profiles/6479734.webp)
Paul Noon
- 516
- 5
- 21