问题描述
如何使用 manage.py 和命令行从数据库中删除所有表?有什么方法可以使用适当的参数执行 manage.py 以便我可以从 .NET 应用程序执行它?
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 from a .NET application?
推荐答案
据我所知,没有删除所有表的管理命令.如果您不介意破解 Python,您可以编写自己的自定义命令来执行此操作.您可能会发现 sqlclear
选项很有趣.文档说 ./manage.py sqlclear
打印给定应用名称的 DROP TABLE SQL 语句.
As far as I know there is no management command to drop all tables. If you don't mind hacking Python you can write your own custom command to do that. You may find the sqlclear
option interesting. Documentation says that ./manage.py sqlclear
Prints the DROP TABLE SQL statements for the given app name(s).
更新:无耻地挪用 @Mike DeSimone 在此答案下方的评论以给出完整答案.
Update: Shamelessly appropriating @Mike DeSimone's comment below this answer to give a complete answer.
./manage.py sqlclear | ./manage.py dbshell
从 django 1.9 开始,它现在是 ./manage.py sqlflush
As of django 1.9 it's now ./manage.py sqlflush
这篇关于如何在 Django 中使用 manage.py CLI 从数据库中删除所有表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!