[Django]-How to output SQL generated by Django Admin

8👍

You can simply set in settings.py this logging configuration to have all SQL queries printed on the console

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG'
        },
    }
}

This is the part responsible for printing

       'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG'
        },
👤glowka

3👍

I use the debug_toolbar: https://pypi.python.org/pypi/django-debug-toolbar

Check it out. It’s worth it.

👤aldux

Leave a comment