python - 在 Django 中使用 manage.py 从 CLI 清除数据库的最简单方法是什么?

标签 python mysql django django-1.3

我正在使用 Django 使用 MySQL 构建网站。现在我正在学习,所以我需要经常更改模型,所以我希望所有表都被清除并创建新表。

syncdb 不会触及现有表。有没有更好的方法来处理这个问题?

最佳答案

如果你不关心数据:

最好的方法是删除数据库并再次运行 syncdb。或者你可以运行:

对于 Django >= 1.5

python manage.py flush

对于 Django <1.5

python manage.py reset appname

(您可以在命令末尾添加 --no-input 以跳过交互式提示。)

如果您确实关心数据:

来自文档:

syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.

https://docs.djangoproject.com/en/dev/ref/django-admin/

引用:常见问题解答 - https://docs.djangoproject.com/en/dev/faq/models/#if-i-make-changes-to-a-model-how-do-i-update-the-database

人们也推荐 South (http://south.aeracode.org/docs/about.html#key-features),但我没有尝试过。

关于python - 在 Django 中使用 manage.py 从 CLI 清除数据库的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485106/

相关文章:

php - 具有自定义列标题的水平 SQL 与垂直 SQL

python - 我可以在 Django 1.7 新的 site_header 设置中包含 Logo 图像吗?

python - H5py:以写入模式重新打开文件会删除以前的数据

python - 检查一个列表中有多少元素包含在第二个列表中,包括重复项

mysql - 查询数据库以查看 Now 是否在两个日期之间

尝试创建表时,Mysql 出现语法错误。我找不到这个错误在哪里

python - 试图从字典列表中删除重复的字典值

python - 什么是 __del__ 方法以及如何调用它?

python - Django 用户发帖到推特

python - Django 模型字段中的子类 timedelta