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
1
vote
1 answer
A tough relationship with Django
Take a look at this model (it's hypothetical):
class Manufacturer(models.Model):
#...
class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer)
#...
class City(models.Model):
#...
class Manager(models.Model):
…

augustomen
- 8,977
- 3
- 43
- 63
1
vote
1 answer
Specifying order_by on Related Field in Django
In Django, on a given model it looks like the RelatedManager may only be set statically (see: docs) but is there any way to set the order_by on the related manager for a given Queryset? I tried using prefetch_related and Prefetch, but that does not…

Alex Rothberg
- 10,243
- 13
- 60
- 120
1
vote
2 answers
Trying to serialize a queryset that uses select_related -- Can't obtain fields of foreignkey model
I'm able to access the correct fields ('author.username') when I execute the query.
But if I run that same query through the serializer, it doesn't print out the select_related model fields.
Should I be specifying all fields, plus the additional…

Chris
- 449
- 1
- 9
- 18
1
vote
0 answers
Django Model always fetch related one to one field
I use polymorphic for two models in django, one of them has a one to one relation to another model.
from polymorphic.models import PolymorphicModel
class Base(PolymorphicModel):
name = models.CharField(max_length=25)
class…

MKM
- 503
- 1
- 7
- 16
1
vote
1 answer
Django: Caching RelatedObjects using select_related
I am trying to cache a Django Model object with the RelatedObjects. But when I access the related objects, a query gets fired. Here is the code:
class Main(models.Model):
name = models.CharField('Name', max_length=100)
class…

rtindru
- 5,107
- 9
- 41
- 59
1
vote
0 answers
django reverse models from single sql
I have 2 models:
class Restaurant(models.Model):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
def __str__(self): # __unicode__ on Python 2
return "%s the…

Ovidiu Iordache
- 11
- 3
1
vote
0 answers
select_related with Proxy models doesn't not work on Django 1.4.22
I have a model structure like this:
app1.models
class App(FunkyClassLoadoerMixin, DateMixin):
user = models.OneToOneField(User, blank=False, null=False, db_index=True, unique=True)
# some other fields
class DateMixin(models.Model):
…

danielmaxx
- 353
- 4
- 12
1
vote
1 answer
django-select2 not working with inlines in django-admin
Here are my models and admin classes:
---------------------Models-----------------------
from django.contrib.auth.models import User
class PurchaseOrder(models.Model):
buyer = models.ForeignKey(User)
is_debit = models.BooleanField(default =…

Gurjot Bhatti
- 977
- 1
- 10
- 24
1
vote
0 answers
Django: All objects AND all M2M related fields in one query
Let's say I have these:
class Publication(models.Model):
title = models.CharField(max_length=128)
author = models.ManyToManyField(Author, through='Authorship')
class Author(models.Model):
first_name =…

Rob L
- 3,634
- 2
- 19
- 38
1
vote
1 answer
django-avatar, 1 query per avatar loaded
I'm displaying a page with around 60 avatars using django-avatar and a query is made for each one of them.
I cannot use User.objects.select_related('avatar') because there is no link between my user and his avatar. So how do I optimize…

valentin
- 2,596
- 6
- 28
- 48
1
vote
1 answer
Left join with django framework
I have these models:
class PartidosFifa(models.Model):
Partido = models.IntegerField(max_length=11, default=0)
PaisL = models.CharField(max_length=250)
Local = models.IntegerField(max_length=11, default=0)
Visita =…

user3638474
- 11
- 1
1
vote
0 answers
Django 1.5 select_related defer with multi table inheritance
EDIT:
I fixed a few typos below
I added a zip file to a small app to demonstrate this problem here. You can download it and run python manage.py testselectrelateddefer after you syncdb and migrate.
I added a couple of observations below
I fix I am…

nlhma
- 81
- 1
- 3
1
vote
2 answers
how does this select_related work in Django?
I saw this example from Django documentation on how to use select_related():
from django.db import models
class City(models.Model):
# ...
pass
class Person(models.Model):
# ...
hometown = models.ForeignKey(City)
class…

eagertoLearn
- 9,772
- 23
- 80
- 122
1
vote
2 answers
Using Django's selected_related, but still resulting in additional database hit
I have a UserProfile model with a OneToOneField to auth.models.User.
I also have an Image model with a ForeignKey to User.
In one of my views, I get a bunch of Images like this:
Image.objects.select_related('user__userprofile')
and later, for each…

Salvatore Iovene
- 2,064
- 1
- 17
- 31
1
vote
0 answers
Count annotation with prefetch_related
Edit: Both of the following snippets actually run fine with no errors! It was a typo elsewhere in my codebase.
Is there a way to write an ORM call such that I can get a count annotation of ManyToMany objects and prefetch_related those…

OregonTrail
- 8,594
- 7
- 43
- 58