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
3
votes
3 answers

How can I get a total count of a model's related objects and the model's children's related objects?

In Django, I've got a Checkout model, which is a ticket for somebody checking out equipment. I've also got an OrganizationalUnit model that the Checkout model relates to (via ForeignKey), as the person on the checkout belongs to an…
Ben Kreeger
  • 6,355
  • 2
  • 38
  • 53
3
votes
1 answer

Django select_related: How to display related table data in a template

I am having trouble displaying fields of related tables in my template when using select_related() Here is my model: class Customer(models.model): customer_name = models.CharField(max_length=500) class Orders(models.model): cust_id =…
Ben
  • 5,085
  • 9
  • 39
  • 58
2
votes
2 answers

Django admin performance issue

I'm getting thousands of these queries when I try to open up a model in the Django admin interface and it's leading to a serious performance issue. [sql] SELECT ... FROM `auth_user` WHERE `auth_user`.`id` = 9535 [sql] (21ms) Found 1 matching…
Charles Offenbacher
  • 3,094
  • 3
  • 31
  • 38
2
votes
2 answers

How to remove duplicates in a django request

Remove duplicates in a Django query Image SQL duplicate. I used select_related and reduced the number of requests from 90 to 18, but if the database grows, then this will not save me. How can the query be improved? views.py def index(request): …
kekai
  • 27
  • 1
  • 4
2
votes
1 answer

How can I mimic 'select_related' using google-appengine and django-nonrel?

django nonrel's documentation states: "you have to manually write code for merging the results of multiple queries (JOINs, select_related(), etc.)". Can someone point me to any snippets that manually add the related data? @nickjohnson has an…
Aaron
  • 4,206
  • 3
  • 24
  • 28
2
votes
1 answer

How to make autocomplete filter in django admin form with class based model

I want to add a dropdown with autocomplete filter such as select2 into django admin form with class based model. i have tried several tricks avilable over the internet but not succeeded. here are some code snippet i have. i want to show all category…
Manish Agarwal
  • 166
  • 2
  • 12
2
votes
1 answer

django annotate on select_related field

My simplified models: class Product(models.Model): name = models.CharField() class Price(models.Model): product = models.OneToOneField('Product', primary_key=True) value = models.DecimalField() class Cart(models.Model): product =…
Maxim Volkomorov
  • 193
  • 3
  • 16
2
votes
1 answer

Django: Exclude field if select_related() gets used

I have a Job and Blob model like this: class Job(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) start_time = models.DateTimeField() input = models.ForeignKey('Blob') class…
guettli
  • 25,042
  • 81
  • 346
  • 663
2
votes
2 answers

Need help using data from a Select_related() queryset

I'm working on a parts database, where every part number can also be an assembly, meaning it is composed of any number of other parts (and the loop can go on, subparts being made of even more parts, etc). So there are two database tables, one for…
j_syk
  • 6,511
  • 2
  • 39
  • 56
2
votes
1 answer

Django get dictionary of siblings without using prefetch related?

I have three models like the following and have a list of Boys. How can I get a list of each of their sisters? Unfortunately I can not use prefetch related as I am stuck using a very old version of django. class Parent(model.Model): name =…
2
votes
1 answer

Get related column on .annotate() data Django

I created this simple set of data to illustrate my point. It is a simple model with no further relations to any other model. I need to group the data above by topicid, find the max date for each group and then get the author for that date. info =…
Shane G
  • 3,129
  • 10
  • 43
  • 85
2
votes
2 answers

Using select_related to get values two models away

Lets say I have the following: class Model1(Model): field1 = ForeignKey(Model2) query_field = IntegerField() class Model2(Model): field2 = ForeignKey(Model3) class Model3(Model) field3 = SomeDesiredValue Now I want to do a query…
sedavidw
  • 11,116
  • 13
  • 61
  • 95
2
votes
1 answer

Get all parent categories Django - Follow foreign key UP

I got the following models: class Category(models.Model): name = models.CharField(max_length=255) parent = models.ForeignKey("self", blank=True, null=True) class Meta: verbose_name = _("category") verbose_name_plural =…
2
votes
1 answer

filter with select_related on Django

I have a problem with the usage of select_related feature of Django with the filter operation, here's my problem, I have three classes : class A: # various fields here class B(models.model): related_A = models.ForeignKey(A) related_C =…
Kobz
  • 469
  • 6
  • 17
1
vote
1 answer

Cannot reduce "SELECT" queries with "select_related()" and "prefetch_related()" in one-to-one relationship in Django

I have Person and PersonDetail models in one-to-one relationship as shown below. *I use Django 3.2.16: class Person(models.Model): name = models.CharField(max_length=20) def __str__(self): return self.name class…
1 2
3
9 10