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
0
votes
1 answer
Additional Foreign Key Query in Django
I am a newbie to django and was reading about select_related. I understand that whenever a foreign key is accessed django executes an additional query. But when I checked with DEBUG log in my code, it seems django executes two queries no matter if…

Chandru Jc
- 105
- 10
0
votes
1 answer
Django model related_list with list view
First, I have a student model and a counseling model.
There are hundreds of consultation models in one student model.
I'd like to make the last consultation date for each subject(classification) into a listview for each student.
If make it ,…

chea
- 101
- 1
- 11
0
votes
0 answers
Override queryset 'only' method | Django
For reducing the number of queries when people access frequently queried foreign key references, I've created a custom manager which includes select_related always for the foreign key relation.
Now that there are places in code where people have…

Santhosh
- 130
- 1
- 2
- 11
0
votes
1 answer
How to find all related items of django foreign key with multi level self relationships?
Given a model:
class Example(models.Model):
name = models.CharField(max_length=50, blank=True)
master= models.ForeignKey('Example', on_delete=models.PROTECT, blank=True, null=True)
With this model, it is possible to have an "Example"…

VolkanOzcan
- 337
- 1
- 9
0
votes
1 answer
how to optimize the query here with select_related?
i have
class A(models.Model):
field_a = models.OneToOneField(B, on_delete=models.CASCADE)
class B(models.Model):
field_b = models.charField()
how to write the most optimum query here using select_related or something else?
In my use case…

coderelliot
- 421
- 5
- 15
0
votes
0 answers
Django queryset using select_related and prefetch_related
models.py
class Topic(Model):
pass
class Question(Model):
topic = models.Foreignkey(Topic)
class Syllabus(Model):
topic = models.Foreignkey(Topic)
#The Bridge Model
class Test_Syllabus(Model):
test = models.Foreignkey(Test)
syllabus =…

user3425929
- 3
- 3
0
votes
1 answer
How to Annotate Specific Related Field Value
I am attempting to optimize some code. I have model with many related models, and I want to annotate and filter by the value of a field of a specific type of these related models, as they are designed to be generic. I can find all instances of the…

Alister
- 453
- 6
- 15
0
votes
1 answer
Django - how to control and filter related objects
I have two models
Note : there are some code i have removed to make the question simple
class Categories(models.Model):
category_name = models.CharField(max_length=100, null=True)
class Courses(models.Model):
course_name …

DAMAR225
- 1,993
- 1
- 13
- 22
0
votes
1 answer
django select_related('created_by').... what is the context variables?
I use select_related, and query log is good.
How can I use the context variables 'posts'?
{{post.username}} is not working.
model.py
class Post(models.Model):
`subject = models.CharField(default='', max_length=255)
content =…
0
votes
1 answer
Django-select2 filtering widget data by owner (Related user)
I'm using django-select2 with class based view (create, update, delete view)
In CreateView, i'm use form_class in view with my form. In this form i'm use widgets for selecting related objects. And i need filtering these objents by created_user…

Дмитрий Тихомиров
- 11
- 3
0
votes
1 answer
Select related executes queries
I can't make select_related work with the following configuration:
Models:
class Text(models.Model):
author = models.ForeignKey('authors.Author',
on_delete=models.SET_NULL,
…

marcanuy
- 23,118
- 9
- 64
- 113
0
votes
1 answer
Relate three models together based on profile field
I want to be able to relate my User model, profile model, and a seperate model together. My current model structure looks like this:
class Profile(models.Model):
COORDINATOR = 1
LEADER = 2
ADMIN = 3
ROLE_CHOICES = (
…

Andrew
- 12,172
- 16
- 46
- 61
0
votes
1 answer
Returning the related field using in views.py Django
I have the following code:
models.py
class Model_ItemName(models.Model):
item_name = models.CharField(max_length = 50) # e.g. "Power Ranger"
class Model_Cart(models.Model):
item_name =…

Fxs7576
- 1,259
- 4
- 23
- 31
0
votes
1 answer
how to do select_related from multiple table
I have 3 models connected in this way:
class book(models.Model):
id = models.AutoField(primary_key=True)
publisher= models.ForeignKey(Publisher, related_name='publisher')
class reader(models.Model):
publisher=…

N.Bar
- 135
- 1
- 1
- 10
0
votes
1 answer
select_related multiple tables
i have 3 models:
class book(models.Model):
id = models.AutoField(primary_key=True)
publisher= models.ForeignKey(Publisher, related_name='publisher')
def __str__(self):
return str(self.publisher)
def __repr__(self):
…

N.Bar
- 135
- 1
- 1
- 10