How to drop all tables from the database with manage.py CLI in Django?

前端 未结 18 1759
余生分开走
余生分开走 2020-11-29 16:54

How can I drop all tables from a database using manage.py and command line? Is there any way to do that executing manage.py with appropriate parameters so I can execute it f

18条回答
  •  孤街浪徒
    2020-11-29 17:52

    Here's an example Makefile to do some nice things with multiple settings files:

    test:
        python manage.py test --settings=my_project.test
    
    db_drop:
        echo 'DROP DATABASE my_project_development;' | ./manage.py dbshell
        echo 'DROP DATABASE my_project_test;' | ./manage.py dbshell
    
    db_create:
        echo 'CREATE DATABASE my_project_development;' | ./manage.py dbshell
        echo 'CREATE DATABASE my_project_test;' | ./manage.py dbshell
    
    db_migrate:
        python manage.py migrate --settings=my_project.base
        python manage.py migrate --settings=my_project.test
    
    db_reset: db_drop db_create db_migrate
    
    .PHONY: test db_drop db_create db_migrate db_reset
    

    Then you can do things like: $ make db_reset

提交回复
热议问题