Questions tagged [django-select-related]

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.

147 questions
1
vote
1 answer

How to prefetch or use select related in reverse generic relation in django?

Assume i have two models: class A: content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT, null=True, blank=True) object_id = models.PositiveIntegerField(null=True, blank=True) attached_object =…
1
vote
1 answer

What is the performance difference in the following two cases of select_related

I have the following two cases. Of the two, I am not able to tell which is the best order to take. Is there a performance issue in either of them?? Case 1 Model.objects.filter(item1=4,item2=5).select_related('store').get(id=1) Case…
fakeMake
  • 738
  • 8
  • 18
1
vote
1 answer

prefetch by related_name in children foreign key django

i'm trying to prefetch related from parent model to chidren throught the related name, However the queryset in the template still hits the DB in PostgreSQL, my modelB modelC and ModelD all point towards modelA and when I overwrite the generic class…
1
vote
1 answer

Django: select_related() / prefetch_related() among 4 tables

I would like to query across 4 tables ( directions, layers, metal stack, and layers). I have the majority of the query done except for one part which I will explain further below. I need a resulting table with the columns layers, directions,…
rellzz
  • 31
  • 3
1
vote
0 answers

Django models DB queries optimisation (using select_related / prefetch_related)

I have this one-to-many relation models [each domain can have several kpis]. i use MySQL DB. class Domains(models.Model): class Meta: managed = True db_table = 'edison_domains' id = models.AutoField(primary_key=True) …
shayms8
  • 671
  • 6
  • 13
  • 28
1
vote
0 answers

Serializing objects runs too much SQL queries

I'm trying to serialize related field set to list. The problem is serializing a QuerySet is very inefficient. class ZipCode(..): city = ForeignKey... code = CharField... class CityManager(models.Manager): def get_queryset(self): …
Milano
  • 18,048
  • 37
  • 153
  • 353
1
vote
1 answer

How to access related values with select_related()

I have a QuerySet that I'm attempting to work with the values on related tables. I see the related tables/values when I run the queryset.query, however I'm not sure how to pull those values for use in forms/tables. Here is my QuerySet…
AlliDeacon
  • 1,365
  • 3
  • 21
  • 35
1
vote
0 answers

How to get all fields of tables related through a foreign key as well as a many to many relationship in django ORM?

I have a table User_Device with fields username, num_of_years and device (foreign key), table Device with fields id, name, type and many to many relationship on field property, which is the third table Property that has fields id, name, location. I…
1
vote
1 answer

Django fetch related child with parent object

I am using Django 2.2 and I have a model with two classes Product and ProductRevision. When I retrieve a Product, or a list of Products, I always fetch the corresponding ProductRevision. ProductRevision objects are incremented and only the last…
1
vote
0 answers

Create a query joining two independent tables, with one Foreign Key in common

I have two Models, one for Intakes of Goods in the warehouse, the other for Output of goods. My available inventory per item is the sum of the intakes minus the sum of the outputs for each item. How can I do that in a query in Django? I have tried…
orcaen7
  • 51
  • 5
1
vote
0 answers

select_related on nullable fields does not work as expected in view

I am using select_nullable in a Django view to retrieve a relationship that can be null nullable foreign keys must be specified So I am explicitely passing it as a parameter: source_text =…
marcanuy
  • 23,118
  • 9
  • 64
  • 113
1
vote
2 answers

In django_select2, the field_id parameter is getting not passed in ajax url

While I am using django_select2, the field_id parameter is not getting passed to get URL select2/fields/auto.json. Due to this, it throws 404 error. Is there any configuration I am missing? python3.6, django2.1 and django_select2 LIB_VERSION =…
1
vote
1 answer

using select_related and prefetch_related with many arguments

I am relatively new to django, and I am using select_related() and prefetch_related() in django to decrease my hits to the database. this is a part of my views.py file: topic =…
arianhf
  • 143
  • 11
1
vote
1 answer

Django ORM select_related rendering template

I have this models in Django 1.10 version class Game(models.Model): game_code = models.CharField(max_length=10) home_team = models.ForeignKey(Team, related_name="home_set", default=2, blank=True, null=True) away_team =…
1
vote
1 answer

Django select_related join model attributes with a single query

I'm trying to find a optimal solution with a single query to retrieve attributes on a join model. I have the following models relationships: class Player(models.Model): first_name = models.CharField(max_length=30) last_name =…
Pietro
  • 1,815
  • 2
  • 29
  • 63