SoFunction
Updated on 2024-12-19

Django shell debugging mods output SQL statement method

Inside, configure logging as follows:

LOGGING = {
 'version': 1,
 'disable_existing_loggers': False,
 'handlers': {
  'console':{
   'level':'DEBUG',
   'class':'',
  },
 },
 'loggers': {
  '': {
   'handlers': ['console'],
   'propagate': True,
   'level':'DEBUG',
  },
 }
}

Effect of use:

$ python  shell
 
In [1]: from  import A
 
In [2]: ()
(0.001) SELECT "app1_a"."id", "app1_a"."name" FROM "app1_a" LIMIT 21; args=()
[<A: A object>]

In a shell command line environment, you can use django-exension's shell_plus command and turn on the --print-sql option.

python shell_plus --print-sql

>>> ()
SELECT "library_author"."id", "library_author"."name" FROM "library_author" LIMIT 21
Execution time: 0.001393s [Database: default]
<QuerySet [<Author: Author object>]>

The built-in logger provided by django

django All message loggers in the Django hierarchy. Instead of posting a message with this name, use one of the following loggers.

Log messages related to request processing. 5xx responses are promoted to error messages; 4xx responses are promoted to warning messages.

Log messages related to the processing of requests received by the server invoked by the RunServer command. an HTTP 5XX response is logged as an error message, a 4XX response is logged as a warning message, and everything else is logged as an INFO.

Log messages related to template rendering

Messages about the code's interaction with the database. For example, each application-level SQL statement requested for execution is logged to this logger at the debug level.

Above this Django shell debugging mods output SQL statement method is all I share with you, I hope to give you a reference, and I hope you support me more.