在 django 中迁移数据的最佳方式是什么
After making some changes in my models (eg. new field in a model and a new model) what is the best way of reflecting these changes to my populated database?
PS: I wanted to see many solutions in one place rated. Apparently more solutions are already listed here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
dumpdata 和 loaddata 参数,在中间杀死数据库:
python manage.py loaddata dump.json
请参阅manage.py 文档 了解更多信息。
Another technique is to use the dumpdata and loaddata arguments to manage.py, killing your database in-between:
python manage.py dumpdata > dump.json
python manage.py loaddata dump.json
See manage.py docs for more.
我在此处提出了类似的问题,并得到了很多答案。
有很多方法可以做到这一点,例如使用 SQL 手动进行转储和重新加载、使用固定装置或使用 Django 的“新兴”模式演化包之一:
(有一个DjangoCon 视频,其中有关于 Django 中的架构演化的小组,其中这 3 个解决方案进行了讨论)
I've asked a similar question here and got quite a few answers.
There are quite a lot of ways of doing it, like manually doing the dumping and reloading with SQL, using fixtures or using one of the "emerging" schema-evolution packages for Django:
(there's a DjangoCon video of a panel on schema-evolution in Django where these 3 solutions are discussed)
取决于变更的范围。 如果超出了 ALTER 的范围,那么您就正在进行大手术。 备份模型和数据库,以便您可以返回。
我的偏好是将您的新(修订、更正、扩展)模型作为新应用程序放入。 它不会有 URL 或任何东西,只有一个模型。
从旧模型中提取数据后,您可以以首选的新格式重建数据库。
Depends on the scope of the changes. If it's beyond an ALTER, you're doing major surgery. Make backups of model as well as database so you can go back.
My preference is to put your new (revised, corrected, expanded) model in as a NEW application. It won't have URL's or anything, just a model.
Once you've pulled the data out of your old model, you can rebuild your DB in the preferred new format.
Django 现在有自己的内置迁移,记录在:
https://docs.djangoproject .com/en/dev/topics/migrations/
Django now has its own built-in migrations, documented at:
https://docs.djangoproject.com/en/dev/topics/migrations/
使用manage.py sqlall 查看新列的参数是什么,并使用Alter table 语句手动将它们添加到数据库中。 这样你就不必重做你的数据库; 不过,它需要一些 SQL 知识...
看看这里(向下滚动到“更改数据库架构”)
Look with manage.py sqlall what the parameters are for the new columns and manually add them in your database with Alter table statements. This way you don't have to redo your database; It requires some SQL knowledge though...
Take a look here (Scroll down to "Making Changes to a Database Schema")
按顺序执行这些步骤可能会对您有所帮助
:有关更多详细信息,
请点击此处:http://south.readthedocs.org/en/latest/
1)
python manage.py schemamigration apps.appname --initial
以上步骤默认创建迁移文件夹。
2)
python manage.py migrate apps.appname --fake
生成假迁移。
3)
python manage.py schemamigration apps.appname --auto
然后您可以根据需要添加字段并执行上述命令。
4)
python manage.py migrate apps.appname
然后将文件迁移到数据库。
Perform these steps in order may help you
:For more details,
clickhere: http://south.readthedocs.org/en/latest/
1)
python manage.py schemamigration apps.appname --initial
Above step creates migration folder as default.
2)
python manage.py migrate apps.appname --fake
generates a fake migration.
3)
python manage.py schemamigration apps.appname --auto
Then you can add fields as you wish and perform the above command.
4)
python manage.py migrate apps.appname
Then migrate the files to the database.