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
3 answers
Django hitting MySQL even after select_related()?
I'm trying to optimize the database calls coming from a fairly small Django app. At current I have a couple of models, Inquiry and InquiryStatus. When selecting all of the records from MySQL, I get a nice JOIN statement on the two tables, followed…

Jack M.
- 30,350
- 7
- 55
- 67
1
vote
2 answers
select_related() with filter() + Q()
class A(models.Model):
pass
class B(models.Model):
a = models.ForeignKey(A)
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
content_object = generic.GenericForeignKey('content_type',…

Юрий Андреев
- 11
- 2
0
votes
1 answer
Django select_related and field lookup
===Models===
class A(models.Model):
name= models.CharField(max_length=20, blank=False)
Class B(models.Model):
university = models.CharField(max_length=25, blank=False)
location = models.CharField(max_length=30, blank=False)
…

thchand
- 358
- 2
- 8
- 20
0
votes
0 answers
How to use select_related to get related data within a model serializer(DRF)
I have 3 models: Product, Trust and Store. Product and Trust both has store as a foreign key. In model serializer of Product how to get data from the trust table?
First I used SerializerMethodField for getting data from the trust, it will make db…

Aswany Mahendran
- 11
- 1
0
votes
2 answers
Django query with related models shows only fields of one model
I have three related models and want to make a query to get a combination of fields from all three models.
invoice_select = Ordered_item.objects.filter(oi_order = o_id).select_related()
generates the SQL-statement which I can check with the…

Helmut
- 233
- 5
- 14
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
1 answer
How to create Django ORM query for Inner Join with Count clause?
I have 2 models named 1.Blogs/Posts 2. PostLike model. I want fetch all blogs with their respected likes counts as well. I have fetch all the blogs data with their likes count in Mysql but I am not aware about the DJANGO ORM query for same
from…

akshay patil
- 1
- 1
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
Recreating JOIN query in django using select related
I am trying to figure out how to recreate the following SQL join in Django:
SELECT taxonomy_request_id, proposed_taxonomy, data_source_field_name, data_source_table_name, data_source_database_name, data_source_server_name
FROM…

BigBlueMonster
- 65
- 4
0
votes
0 answers
How to dynamically filter a ModelChoiceField - Django
I am very new very mew to django... I am trying to filter the ModelChoiceField in my 'Detalle_ordenForm' so that it only shows the 'pacientes' (patients in english) that belong to a particular 'propietario' (owner in english)
my models are (only…

RBM
- 1
0
votes
1 answer
chained select_related from many tables in django
Here I have the following models:
class GenericUserData(AbstractUser):
username = None
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
email = models.EmailField(max_length=100, unique=True)
date_joined =…

john
- 3
- 1
0
votes
0 answers
Create a conditional prefetch_related/select_related with Django querysets
I want to only execute a prefect_related on my queryset when a boolean value in my database is True.
I have the following models:
from django.db import models
class Map(models.Model):
has_ratings = models.BooleanField()
class…

TimS
- 11
- 2
0
votes
1 answer
How do I use django templates to get a field from the parent object of a model?
I'm trying to create an interface where students can match with other students in the class for a group project based on shared interests and I have a queryset in a django view and from there I am able to use select_related to get the parent object…

a.jones
- 13
- 4
0
votes
0 answers
Using Django formset displaying related names in template
I am trying to display the state values for each country name in Django app. To save the user response, I am using Django generic CreateView. My models look something like this:
class Question(model.Models):
ques_id =…

Love Putin Not War
- 552
- 6
- 20
0
votes
1 answer
When "select_related" is needed?
In my project , Each candidate can takepart in some assessments,each assessment has some tests, each test has some questions in it and candidates should answer the questions
at last scores of the questions are saved in question_score and test_score…

Z.G.
- 5
- 2