在 django 中迁移数据的最佳方式是什么

发布于 2024-07-06 13:51:14 字数 211 浏览 6 评论 0原文

在我的模型中进行一些更改(例如模型中的新字段和新模型)后,将这些更改反映到我填充的数据库中的最佳方式是什么?


PS:我想在一个地方看到许多解决方案的评级。 显然,此处已经列出了更多解决方案。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

稚然 2024-07-13 13:51:14

dumpdata 和 loaddata 参数,在中间杀死数据库:

  1. 另一种技术是使用 manage.py的 dump.json
  2. 使用外部工具,删除任何受影响的表,或终止整个数据库
  3. 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:

  1. python manage.py dumpdata > dump.json
  2. With an external tool, drop any affected tables, or kill the whole db
  3. python manage.py loaddata dump.json

See manage.py docs for more.

无所的.畏惧 2024-07-13 13:51:14

我在此处提出了类似的问题,并得到了很多答案。

有很多方法可以做到这一点,例如使用 SQL 手动进行转储和重新加载、使用固定装置或使用 Django 的“新兴”模式演化包之一:

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:

转身泪倾城 2024-07-13 13:51:14

取决于变更的范围。 如果超出了 ALTER 的范围,那么您就正在进行大手术。 备份模型和数据库,以便您可以返回。

我的偏好是将您的新(修订、更正、扩展)模型作为新应用程序放入。 它不会有 URL 或任何东西,只有一个模型。

  1. 创建新模型作为新应用程序。 创建测试,只是为了确保它有效。
  2. syncdb 来构建新模型的临时实现。
  3. 编写一个小的一次性实用程序来查询旧模型,并加载新模型。 您可能想在纯 SQL 中尝试此操作。 我更喜欢编写一个简单的查询、构建和保存循环。
  4. 加载新模型后,您可以将其转储到 JSON 文件。

从旧模型中提取数据后,您可以以首选的新格式重建数据库。

  1. 将新模型移至您现有的应用程序中。
  2. 删除应用程序表的旧版本。
  3. syncdb 来构建新表。
  4. 加载包含数据的 JSON 文件。

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.

  1. Creating the new model as a new application. Create tests, just to be sure it works.
  2. syncdb to build this interim implementation of the new model.
  3. Write a little one-time utility to query your old model, and load your new model. You might want to try this in pure SQL. I prefer to write a simple query, build and save loop.
  4. After the new model is loaded, you can dump this to a JSON file.

Once you've pulled the data out of your old model, you can rebuild your DB in the preferred new format.

  1. Move the new model into your existing application.
  2. Drop the old versions of the application's tables.
  3. syncdb to build the new tables.
  4. Load the JSON file with the data.
若能看破又如何 2024-07-13 13:51:14

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/

明天过后 2024-07-13 13:51:14

使用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")

失眠症患者 2024-07-13 13:51:14

按顺序执行这些步骤可能会对您有所帮助

有关更多详细信息,

请点击此处: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文