0

I want to log the raw SQL statements in my Django testcases.
I can see the INSERT sql statements, but I can't see the SELECT sql statements in the log.

I want to see every SQL statement in the log, whether it is CREATE, SELECT, UPDATE or something else.

Output

$ python manage.py test
<OUTPUT OMITTED>
Found 1 test(s).
Running tests...
----------------------------------------------------------------------

[{'sql': 'INSERT INTO "myapp_testtabletwo" ("test_field") VALUES (\'abc\') RETURNING "myapp_testtabletwo"."id"', 'time': '0.001'}]
.
----------------------------------------------------------------------
Ran 1 test in 0.113s

OK

Destroying test database for alias 'default'...
Closing active connection

tests.py

from django.db import connection
from my_app.models import TestTableTwo

class DatabaseExampleTests(TestCase):
    databases = '__all__'

    def test_example(self):
        with CaptureQueriesContext(connection) as ctx:
            created_object = TestTableTwo.objects.create(test_field="abc")
            all_objects = TestTableTwo.objects.all()
        print(ctx.captured_queries)

models.py

from django.db import models

class TestTableTwo(models.Model):
    id = models.AutoField(primary_key=True)
    test_field = models.CharField(max_length=100, blank=True, null=True)

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "testpostgres",
        'USER': "postgres,
        'PASSWORD': "password",
        'HOST': "postgres"
    }
}

Version

$ python -V
Python 3.9.15

$ pip list 
# output omitted
Package                Version
---------------------- ---------
Django                 4.1
psycopg2               2.9.5

Edit:
When I change all_objects = TestTableTwo.objects.all() to print(TestTableTwo.objects.all()), I see the SELECT sql statement in the log.
But I don't understand the reason, why it works with the print() statement.

Ewro
  • 447
  • 4
  • 13
  • you mean whenever there's query made from `ORM` you want to see that `raw sql` in terminal? – Hemal Patel Jan 16 '23 at 12:40
  • @HemalPatel Correct, my expectation is to see each `raw sql` in `ctx.captured_queries` – Ewro Jan 16 '23 at 12:41
  • i think you can use this to watch all the queries in The Terminal. [Here](https://stackoverflow.com/a/20161527/16250404) Yes it works i just tried. – Hemal Patel Jan 16 '23 at 12:43
  • @HemalPatel Unfortunately I got the same behavior. I can see the INSERT statements, but can't see the SELECT statement in the logs. I tried your example – Ewro Jan 16 '23 at 12:49

1 Answers1

0

here put this in settings.py: you can check select queries in the terminal.

import os

LOGGING = {
    'version': 1,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['console'],
        }
    }
}
Hemal Patel
  • 878
  • 6
  • 16
  • Unfortunately, I got the same behavior. I cann see INSERT, but not SELECT `(0.001) INSERT INTO "myapp_testtabletwo" ("test_field",) VALUES ('abc',) RETURNING "myapp_testtabletwo"."id"; args=('abc',); alias=default` – Ewro Jan 16 '23 at 12:59
  • @Ewro Weird :'). i just created demo project and it showing all the queries. – Hemal Patel Jan 16 '23 at 13:00
  • 1
    Strangely enough, I can see the SELECT query, when I use print() `print(TestTableTwo.objects.all())` – Ewro Jan 16 '23 at 13:06
  • @Ewro my bad. I was using it the same way. Just noticed. You have to use `print`. Still will check if I can find a better way. – Hemal Patel Jan 16 '23 at 13:11