What is the django command to delete all tables?

前端 未结 7 2003
清歌不尽
清歌不尽 2021-01-31 16:47

Are there django commands that

A. Delete all tables

B. delete all data in all tables

C. Create all tables as defined in the model?

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 17:01

    If you have the client libraries installed for your database, you can run this:

    python manage.py sqlflush | python manage.py dbshell

    This doesn't drop the tables, but truncates them.

    There isn't a command that does the it all in one go, but this "one liner" will drop all the tables and then re-create them. It would only work if you were running on a system that provides these utilities at the shell.

    echo 'from django.conf import settings; print settings.INSTALLED_APPS; quit();' | python manage.py shell --plain 2>&1 | tail -n1 | sed -r "s|^.*\((.*)\).*$|\1|; s|[',]| |g; s|django\.contrib\.||g" | xargs python manage.py sqlclear | python manage.py dbshell && python manage.py syncdb

提交回复
热议问题