将多个模型从一个django应用程序移动到另一个(Moving multiple models from one django app to another)

编程入门 行业动态 更新时间:2024-10-26 23:25:50
多个模型从一个django应用程序移动到另一个(Moving multiple models from one django app to another)

我正在使用django v2.0.2构建一个项目,该项目包含3个24个模型的应用程序。 其中一个应用程序有14个型号。 在一个应用程序中拥有如此多的模型变得越来越复杂,我想创建一个新的应用程序并将少数模型移动到这个应用程序。

我找到了一个答案,解释了如何使用南方来完成。 我一直在使用django核心迁移,因为南方已被弃用,我现在不想转向南方。

我想要移动的模型非常复杂 - 它们有ForeignKey字段, ManyToMany字段等。我需要一个工作流程来展示如何使用django核心迁移来移动这些模型。

I'm building a project using django v2.0.2 that consists of 3 apps with 24 models. One of the apps has 14 models. Having so many models in one app is becoming complicated, and I'd like to create a new app and move few models to this app.

I found an answer explaining how this can be done using south. I've been using django core migrations and since south is deprecated, I don't want to switch to south at this point.

The models I want to move are quite complex - they have ForeignKey fields, ManyToMany fields, etc. I need a workflow showing how I can move these models using django core migrations.

最满意答案

这不应该太难。 您遇到的主要问题是Django会自动从模型类的名称和包含它的应用程序派生数据库表的名称。

这意味着除非您在模型元中显式设置db_table属性,否则将根据应用程序/模型名称组合自动生成与模型对应的所有表的名称。

因此,如果您在博客应用中有Post模型,则该Post模型的自动生成的表名称为blog_post 。 如果您决定将Post模型移动到Article应用程序,Django将寻找名为article_post的表,并且无法将您的模型与正确的表关联。

要解决此问题,您需要在每个模型上显式设置db_table属性。 db_table名称值必须与当前的app / model组合对应。 因此,对于上面提到的Post模型,您可以将db_table设置为blog_post 。 设置db_table属性后,可以将模型移动到任何其他应用程序。

class Post(models.Model): title = models.CharField(max_length=120) class Meta: db_table = 'blog_post'

移动所有模型后,登录django admin以验证您的数据是否仍可通过新移动的模型访问。

在移动模型后第一次运行makemigrations时,Django迁移将创建迁移文件,删除旧应用中移动的模型,并在新应用中重新创建它们。 不要应用这些迁移,而是运行migrate --fake ,它会将迁移记录为已执行而不进行任何数据库更改。 之后,将同步您的迁移文件和数据库结构。

That shouldn't be too difficult to do. The main issue you run into is that Django automatically derives the name of the database table from the name of your model class and the app that contains it.

That means unless you're explicitly setting the db_table property in the model meta, the names of all the tables corresponding to your models are auto generated based on app/model name combo.

So if you have a Post model in a Blog app, the auto generated table name of that Post model is blog_post. And if you decide to move Post model to the Article app, Django will be looking for a table named article_post and won't be able to associate your model with the correct table.

To solve this issue, you need to explicitly set the db_table property on each of the models. The db_table name value will have to correspond to the current app/model combo. So in the case of Post model mentioned above, you would set db_table to blog_post. Once you set the db_table property, you can move the model to any other apps.

class Post(models.Model): title = models.CharField(max_length=120) class Meta: db_table = 'blog_post'

Once you move all the models around, login to django admin to verify that your data is still accessible through the newly moved models.

When you run makemigrations for the first time after moving the models, Django migrations will create migration files that deletes the moved models in their old app and recreate them in their new app. Do not apply these migrations and instead run migrate --fake which will record the migrations as having been executed without making any of the database changes. After that, your migration files and your database structure will be synced.

更多推荐

本文发布于:2023-07-05 01:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   应用程序   模型   django   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!