项目常用方法2,方法1可用于分析执行速度,执行时间。
① pip install django-extensions
② INSTALLED_APPS = [
'django\_extensions',
]
③ python manage.py shell_plus --print-sql
INSTALLED_APPS = [
...
'django_extensions', # sql log出力
]
(venv) PS D:\PycharmProjects\pj> python manage.py shell_plus --print-sql
......
>>> User.objects.only('username','first_name','email')
SELECT "auth_user"."id",
"auth_user"."username",
"auth_user"."first_name",
"auth_user"."email"
FROM "auth_user"
LIMIT 21
Execution time: 0.004991s [Database: default]
<QuerySet [<User: purchase007>, <User: purchase001>, <User: purchase005>, <User: purchase008>, <User: purchase004>, <User: purchase006>, <User: admin>, <User: purchase003>, <User: purchase002>]>
「django.db.backends」设定,启动website后,执行的sql都会打印到终端
import os
ROOT_LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
"formatters": {
"simple": {
"format": "%(asctime)s [%(levelname)s] %(message)s"
},
"verbose":{
"format": "%(asctime)s [%(levelname)s] P%(process)d t%(thread)d %(pathname)s:%(lineno)d %(message)s"
}
},
'handlers': {
'console_simple': {
'class': 'logging.StreamHandler',
"formatter": "simple",
},
'console_verbose': {
'class': 'logging.StreamHandler',
"formatter": "verbose",
},
},
'root': {
'handlers': ['console_verbose'],
'level': ROOT_LOG_LEVEL,
},
'loggers': {
'django': {
'handlers': ['console_verbose'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
'propagate': False,
},
'django.db.backends': {
'handlers': ['console_verbose'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
'propagate': False,
},
},
上边配置了很多参数,以下为简化版。
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。