Django - South - Is There a way to view the SQL it runs?

后端 未结 5 1951
遥遥无期
遥遥无期 2021-02-02 09:19

Here\'s what I want to do.

Develop a Django project on a development server with a development database. Run the south migrations as necessary when I change the model.<

5条回答
  •  盖世英雄少女心
    2021-02-02 10:00

    When I need to see the SQL that South generates for debugging or verification I just add the following logging settings to my local_settings.LOGGING.loggers:

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

    This is a complete example of the logging setting for South:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'verbose': {
                'format': '[%(asctime)s] %(levelname)s %(name)s %(lineno)d "%(message)s"'
            },
            'simple': {
                'format': '%(levelname)s %(message)s'
            },
        },
        'filters': {
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse'
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose',
            },
        },
        'loggers': {
            'django': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': True,
            },
            'django.db.backends': {
                'handlers': ['console'],
                'level': 'DEBUG',
            },
        }
    }
    

    This will output everything including the query that South runs to decide what migrations to run:

    [2014-03-12 23:47:31,385] DEBUG django.db.backends 79 "(0.001) SELECT `south_migrationhistory`.`id`, `south_migrationhistory`.`app_name`, `south_migrationhistory`.`migration`, `south_migrationhistory`.`applied` FROM `south_migrationhistory` WHERE `south_migrationhistory`.`applied` IS NOT NULL ORDER BY `south_migrationhistory`.`applied` ASC; args=()"
    

    That and setting verbosity to 2 or 3 is usually more than enough to get a clear picture of what's going on.

提交回复
热议问题