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

后端 未结 5 2088
遥遥无期
遥遥无期 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 09:48

    You could try logging the SQL queries in db.connection.queries, using a management command that calls the migrate with a dry-run option:

    
    from django.core.management.base import BaseCommand
    from django import db
    
    class Command(BaseCommand):
        help = 'Output SQL for migration'
    
        def handle(self, *app_labels, **options):
            # assumes DEBUG is True in settings
            db.reset_queries()
    
            from django.core.management import call_command
            kw = {'db-dry-run': 1,  'verbosity': 0}
            call_command('migrate', **kw)
    
            for query in db.connection.queries:
                print query['sql']
    

    Assuming that south puts everything through the usual db interface that should work. There will be a few extra selects in there when it queries the history table.

    You'd put that in a management/commands/print_migration_sql.py inside your app and then run it:

    
    python manage.py print_migration_sql
    

    It could probably be easily extended to run this only for specific apps etc

提交回复
热议问题