<legend id='Z7vaK'><style id='Z7vaK'><dir id='Z7vaK'><q id='Z7vaK'></q></dir></style></legend>
        <bdo id='Z7vaK'></bdo><ul id='Z7vaK'></ul>

      1. <tfoot id='Z7vaK'></tfoot>

        <small id='Z7vaK'></small><noframes id='Z7vaK'>

        <i id='Z7vaK'><tr id='Z7vaK'><dt id='Z7vaK'><q id='Z7vaK'><span id='Z7vaK'><b id='Z7vaK'><form id='Z7vaK'><ins id='Z7vaK'></ins><ul id='Z7vaK'></ul><sub id='Z7vaK'></sub></form><legend id='Z7vaK'></legend><bdo id='Z7vaK'><pre id='Z7vaK'><center id='Z7vaK'></center></pre></bdo></b><th id='Z7vaK'></th></span></q></dt></tr></i><div id='Z7vaK'><tfoot id='Z7vaK'></tfoot><dl id='Z7vaK'><fieldset id='Z7vaK'></fieldset></dl></div>

        如何在两个 Django 应用程序之间移动模型(Django 1.7)

        时间:2023-06-03

          1. <tfoot id='4axGo'></tfoot>

            <i id='4axGo'><tr id='4axGo'><dt id='4axGo'><q id='4axGo'><span id='4axGo'><b id='4axGo'><form id='4axGo'><ins id='4axGo'></ins><ul id='4axGo'></ul><sub id='4axGo'></sub></form><legend id='4axGo'></legend><bdo id='4axGo'><pre id='4axGo'><center id='4axGo'></center></pre></bdo></b><th id='4axGo'></th></span></q></dt></tr></i><div id='4axGo'><tfoot id='4axGo'></tfoot><dl id='4axGo'><fieldset id='4axGo'></fieldset></dl></div>

                <tbody id='4axGo'></tbody>
              <legend id='4axGo'><style id='4axGo'><dir id='4axGo'><q id='4axGo'></q></dir></style></legend>
                  <bdo id='4axGo'></bdo><ul id='4axGo'></ul>
                • <small id='4axGo'></small><noframes id='4axGo'>

                  本文介绍了如何在两个 Django 应用程序之间移动模型(Django 1.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以大约一年前,我开始了一个项目,和所有新开发人员一样,我并没有真正关注结构,但是现在我与 Django 一起走得更远,它开始出现我的项目布局主要是我的模型结构可怕.

                  So about a year ago I started a project and like all new developers I didn't really focus too much on the structure, however now I am further along with Django it has started to appear that my project layout mainly my models are horrible in structure.

                  我的模型主要保存在一个应用程序中,实际上这些模型中的大多数应该在它们自己的单独应用程序中,我确实尝试解决了这个问题并将它们向南移动,但是由于外键等,我发现这很棘手而且非常困难.

                  I have models mainly held in a single app and really most of these models should be in their own individual apps, I did try and resolve this and move them with south however I found it tricky and really difficult due to foreign keys ect.

                  然而,由于 Django 1.7 和对迁移的内置支持,现在有更好的方法来做到这一点吗?

                  However due to Django 1.7 and built in support for migrations is there a better way to do this now?

                  推荐答案

                  我正在删除旧答案,因为可能会导致数据丢失.正如 ozan 提到的,我们可以在每个应用程序中创建 2 个迁移.这篇文章下面的评论是指我的旧答案.

                  I am removing the old answer as may result in data loss. As ozan mentioned, we can create 2 migrations one in each app. The comments below this post refer to my old answer.

                  第一次迁移以从第一个应用程序中删除模型.

                  First migration to remove model from 1st app.

                  $ python manage.py makemigrations old_app --empty
                  

                  编辑迁移文件以包含这些操作.

                  Edit migration file to include these operations.

                  class Migration(migrations.Migration):
                  
                      database_operations = [migrations.AlterModelTable('TheModel', 'newapp_themodel')]
                  
                      state_operations = [migrations.DeleteModel('TheModel')]
                  
                      operations = [
                        migrations.SeparateDatabaseAndState(
                          database_operations=database_operations,
                          state_operations=state_operations)
                      ]
                  

                  第二次迁移取决于第一次迁移并在第二个应用程序中创建新表.将模型代码移至第二个应用程序后

                  Second migration which depends on first migration and create the new table in 2nd app. After moving model code to 2nd app

                  $ python manage.py makemigrations new_app 
                  

                  并将迁移文件编辑为这样的内容.

                  and edit migration file to something like this.

                  class Migration(migrations.Migration):
                  
                      dependencies = [
                          ('old_app', 'above_migration')
                      ]
                  
                      state_operations = [
                          migrations.CreateModel(
                              name='TheModel',
                              fields=[
                                  ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                              ],
                              options={
                                  'db_table': 'newapp_themodel',
                              },
                              bases=(models.Model,),
                          )
                      ]
                  
                      operations = [
                          migrations.SeparateDatabaseAndState(state_operations=state_operations)
                      ]
                  

                  这篇关于如何在两个 Django 应用程序之间移动模型(Django 1.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL 在逗号列表中搜索 下一篇:MySQL 分片方法?

                  相关文章

                • <legend id='jLvWy'><style id='jLvWy'><dir id='jLvWy'><q id='jLvWy'></q></dir></style></legend>

                      <small id='jLvWy'></small><noframes id='jLvWy'>

                    1. <i id='jLvWy'><tr id='jLvWy'><dt id='jLvWy'><q id='jLvWy'><span id='jLvWy'><b id='jLvWy'><form id='jLvWy'><ins id='jLvWy'></ins><ul id='jLvWy'></ul><sub id='jLvWy'></sub></form><legend id='jLvWy'></legend><bdo id='jLvWy'><pre id='jLvWy'><center id='jLvWy'></center></pre></bdo></b><th id='jLvWy'></th></span></q></dt></tr></i><div id='jLvWy'><tfoot id='jLvWy'></tfoot><dl id='jLvWy'><fieldset id='jLvWy'></fieldset></dl></div>
                    2. <tfoot id='jLvWy'></tfoot>
                        <bdo id='jLvWy'></bdo><ul id='jLvWy'></ul>