0

I have person table as shown below:

# "store/models.py"

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=30)

And, I have test view with print(Person.objects.all()) as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    print(Person.objects.all()) # Here

    return HttpResponse("Test")

Then, when I run test view, SELECT query is run as shown below. *I used PostgreSQL and these logs below are the query logs of PostgreSQL. You can check On PostgreSQL, how to log SQL queries with transaction queries such as "BEGIN" and "COMMIT":

enter image description here

Now, I replace print(Person.objects.all()) with Person.objects.all() as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    Person.objects.all() # Here
    # print(Person.objects.all())

    return HttpResponse("Test")

Or, I replace print(Person.objects.all()) with persons = Person.objects.all() as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    persons = Person.objects.all() # Here
    # print(Person.objects.all()) 

    return HttpResponse("Test")

Then, when I run test view, SELECT query isn't run as shown below:

enter image description here

So, why isn't SELECT query run in Django?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • 5
    [QuerySets are lazy](https://docs.djangoproject.com/en/4.1/topics/db/queries/#querysets-are-lazy). A queryset does not actually query the database unless it is evaluated. – Nick ODell Oct 21 '22 at 16:09
  • Does this answer your question? [does filtering queryset in django query database every time?](https://stackoverflow.com/questions/64579789/does-filtering-queryset-in-django-query-database-every-time) also related: [when exactly django query execution occures](https://stackoverflow.com/questions/67003832/when-exactly-django-query-execution-occures) – Abdul Aziz Barkat Oct 22 '22 at 17:48
  • This might be a better target for any gold badgers passing by: [When are Django Querysets executed in the view?](https://stackoverflow.com/questions/70038002/when-are-django-querysets-executed-in-the-view) – Abdul Aziz Barkat Oct 22 '22 at 17:58

1 Answers1

1

QuerySets are lazy says as shown below so SELECT query isn't run when you run test view:

Django won’t actually run the query until the QuerySet is evaluated.

In addition, there is also the example which SELECT query is not run. I have person table as shown below:

# "store/models.py"

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=30)

And, I have test view with print(Person.objects.all()) as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    print(Person.objects.all()) # Here

    return HttpResponse("Test")

Then, when I run test view, SELECT query is run as shown below:

enter image description here

Now, I replace print(Person.objects.all()) with print(Person.objects.all().create(name="Tom")) as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    print(Person.objects.all().create(name="Tom")) # Here
    # print(Person.objects.all())

    return HttpResponse("Test")

Then, when I run test view, SELECT query isn't run. Instead, INSERT query is run as shown below:

enter image description here

So, to run both SELECT and INSERT queries, we need to separate print(Person.objects.all().create(name="Tom")) as shown below:

# "store/views.py"

from django.http import HttpResponse
from .models import Person

def test(request):
    print(Person.objects.all()) # Here
    Person.objects.create(name="Tom") # Here

    return HttpResponse("Test")

Then, when I run test view, both SELECT and INSERT queries are run as shown below:

enter image description here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129