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
7
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):
…

Paul Noon
- 656
- 1
- 8
- 25
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…

Rob Crowell
- 1,447
- 3
- 15
- 25
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…

rfrankel
- 675
- 8
- 20
5
votes
1 answer
Most efficient way to retrieve user with userprofile in Django
In Django, on a recommended setup, a UserProfile instance is linked by a OneToOneField with its User instance.
class UserProfile(models.Model):
user = models.OneToOneField(User)
data = ...
What is the most efficient way inside a view for…

Simon Steinberger
- 6,605
- 5
- 55
- 97
5
votes
3 answers
django: select_related with entry_set
Should entry_set be cached with select_related? My DB is still getting calls even after I use select_related. The pertinent sections
class Alias(models.Model):
achievements = models.ManyToManyField('Achievement', through='Achiever')
def…

Paul Tarjan
- 48,968
- 59
- 172
- 213
4
votes
1 answer
How to prefetch_related fields for a related field in Django
I have seem some examples of how to prefetch_related fields in a forward and backward relationship in Django, but I have doubts about how can this be applied if we want to prefetch all the fields of a related model.
For instance if I want to fetch…

Ander
- 5,093
- 7
- 41
- 70
4
votes
3 answers
Django-select2 throwing 'Results cannot be loaded.'
I'm trying to deploy my django application in a Droplet virtual machine (DigitalOcean) following this guide.
For this purpose I've used nginx and gunicorn with success.
The problem I'm facing is with django-select2 and is that the widget of the form…

Alessio Ferri
- 345
- 3
- 17
4
votes
1 answer
Django : How to use select_related for a OneToOneField?
I have created a OneToOneField(parent) in Child model with related_name='children'. In my views, I used select_related to get the queryset. But in my page the list of children associated to a parent shows empty.
Models.py:
class…

sumanth
- 751
- 2
- 15
- 34
4
votes
1 answer
Django admin change-list optimizing query: select field1, field2 instead of select *
I have a large horizontal table (30 fields) with quite a few foreign keys (each averaging upto 10 fields). While displaying the table in the Django admin, I use select related to optimize and avoid multiple querying. What I am looking for is to…

tjazz
- 121
- 1
- 7
3
votes
1 answer
Django: select_related() and memory usage
I am working on an API, and I have a question. I was looking into the usage of select_related(), in order to save myself some database queries, and indeed it does help in reducing the amount of database queries performed, on the expense of bigger…

Charalambos Paschalides
- 490
- 1
- 6
- 13
3
votes
2 answers
django - dynamic select fields in forms
I have a model called picks that allows users to select an nfl team (from a static list). Whenever they choose a team, they can no longer choose that team again, so there selection choices are reduced by any teams they have chosen.
I have a…

user417918
- 823
- 2
- 11
- 15
3
votes
1 answer
Django: select_related to a table vs table's field
I have 2 models
class A(models.Model):
val = models.IntegerField()
class B(models.Model):
val2 = models.IntegerField()
a = models.ForeignKey(A)
class C(models.Model):
b = models.ForeignKey(B)
val3 = models.IntegerField()
How…

Pankhuri Agarwal
- 764
- 3
- 23
3
votes
0 answers
How can I use select_related in rest framework?
I am new in Django, I am creating a basic website where users can post some messages and add some tags to that post. I have 3 models Person, Post, and Tag. Using the rest framework, I created API for Posts, which should give me a list of posts with…

Akash
- 559
- 6
- 17
3
votes
1 answer
Django - Optimize queries with select_related()
I have the following model.
class Car(models.Model):
owner = models.ForeignKey('Driver')
class Country(models.Model)
name = models.CharField(max_length=255)
class Driver(models.Model):
name = models.CharField(max_length=255)
age =…

srjjio
- 930
- 1
- 8
- 16
3
votes
1 answer
Django: Display many-to-many fields in the change list
Django doesn't support displaying of related objects from a many-to-many relation in the changelist for a good reason. It would result in a lot of database hits.
But sometimes it is inevitable and necessary to e.g. display an object's categories,…

Bernhard Vallant
- 49,468
- 20
- 120
- 148