Django - reduce the number of queries in ORM
NickName:user1187968 Ask DateTime:2016-06-22T11:57:46

Django - reduce the number of queries in ORM

I used related_name in a Django template to look the foreign key record, and call the count method. Because I have so many "Main" record, the for loop inside the template will create too many queries to the database. If there an easy way for me to reduce the number of queries to the database? Please see below for my setup.

# models.py  
class Main(models.Model):
    name = models.CharField(_('Name'), max_length=255)  

class Sub1(models.Model):
    main = models.ForeignKey(Main, on_delete=models.CASCADE)
    name = models.CharField(_('Name'), max_length=255) 

class Sub2(models.Model):
    main = models.ForeignKey(Main, on_delete=models.CASCADE)
    name = models.CharField(_('Name'), max_length=255) 

class Sub3(models.Model):
    main = models.ForeignKey(Main, on_delete=models.CASCADE)  
    name = models.CharField(_('Name'), max_length=255)     

# views.py
def get_main(request): 
    main_list = Main.objects.all()
    ...

# template 
{% for main in main_list %}    
        {{main.sub1_set.count}}
        {{main.sub2_set.count}}
        {{main.sub3_set.count}}
{% endfor %}

Copyright Notice:Content Author:「user1187968」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37958296/django-reduce-the-number-of-queries-in-orm

More about “Django - reduce the number of queries in ORM” related questions

Django - reduce the number of queries in ORM

I used related_name in a Django template to look the foreign key record, and call the count method. Because I have so many "Main" record, the for loop inside the template will create too many queri...

Show Detail

django to snowflake connection and running ORM queries

I am looking to shift out of Postgres to SnowFlake as some of my features require run time analysis which is faster in Snowflake. I could only get the Python connector API for Snowflake which would

Show Detail

Django - Raw SQL Queries or Django QuerySet ORM

I know Django Object Relational Mapper (ORM) helps bridge the gap between the database and our code Performing raw queries. But I want to find out which is better - Raw SQL Queries or Django Quer...

Show Detail

Large number of queries in Django views

I have created a Django app and everything seems to be working nicely. I then started looking at the Django debug toolbar and saw that some of my pages are generating over 100 queries, with a lot of

Show Detail

Can you do SQL queries on the table generated by Django ORM?

I have a question concerning using Django ORM alongside SQL queries from outside django environment. Namely I would like to make SQL queries on some tables managed by Django (tables corresponding to

Show Detail

The best practice for writing Django apps while optimize ORM queries?

Writing a complex django apps involves playing with ORM objects in different places (views, templates, etc). However, designing ORM models is usually done in the beginning of project. On that stage...

Show Detail

Django ORM: Joining QuerySets

I'm trying to use the Django ORM for a task that requires a JOIN in SQL. I already have a workaround that accomplishes the same task with multiple queries and some off-DB processing, but I'm not

Show Detail

Django ORM queryset on nested queries

I can't figure out how the default Django ORM processes queries like: Model.objects.filter(foreign__field=value) Does it make a lookup on each row, or is it smart enough to resolve the foreign fi...

Show Detail

How to know what queries django orm is creating while saving data?

I'm new to django and dont know much about sql. I have made a model and its working fine. I'm saving data through django admin with no issues. However I have to save lots of data one by one. Is the...

Show Detail

Reducing number of ORM queries in Django web application

I'm trying to improve the performance of one of my Django applications to make them run just a bit smoother, as part of a first iteration in improving what I currently have running. When doing some

Show Detail