问题:如何在两个Django应用之间移动模型(Django 1.7)
因此,大约一年前,我开始了一个项目,像所有新开发人员一样,我并没有真正专注于结构,但是现在我与Django一起走得更远,它开始似乎表明我的项目布局主要是我的模型在结构上很糟糕。
我的模型主要保存在单个应用程序中,实际上这些模型中的大多数应该放在自己的单个应用程序中,我确实尝试解决了此问题并将其向南移动,但是由于外键等原因,我发现它很棘手,而且确实很困难。
但是,由于Django 1.7并内置了对迁移的支持,现在有更好的方法吗?
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.
However due to Django 1.7 and built in support for migrations is there a better way to do this now?
回答 0
我正在删除旧答案,因为这可能会导致数据丢失。如ozan所述,我们可以在每个应用中创建2个迁移。这篇文章下面的评论指的是我的旧答案。
第一次迁移以从第一个应用中删除模型。
$ python manage.py makemigrations old_app --empty
编辑迁移文件以包括这些操作。
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)
]
第二次迁移取决于第一次迁移并在第二个应用程序中创建新表。将模型代码移至第二个应用程序后
$ python manage.py makemigrations new_app
然后将迁移文件编辑为类似的内容。
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)
]
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)
]
回答 1
使用可以很容易地做到这一点migrations.SeparateDatabaseAndState
。基本上,我们使用数据库操作来同时重命名表,同时使用两个状态操作从一个应用程序的历史记录中删除模型并在另一个应用程序的历史记录中创建模型。
从旧应用中删除
python manage.py makemigrations old_app --empty
在迁移中:
class Migration(migrations.Migration):
dependencies = []
database_operations = [
migrations.AlterModelTable('TheModel', 'newapp_themodel')
]
state_operations = [
migrations.DeleteModel('TheModel')
]
operations = [
migrations.SeparateDatabaseAndState(
database_operations=database_operations,
state_operations=state_operations)
]
添加到新应用
首先,将模型复制到新应用的model.py中,然后:
python manage.py makemigrations new_app
这将生成一个迁移CreateModel
操作,其中天真的操作是唯一的操作。将其包装在一个SeparateDatabaseAndState
操作中,这样我们就不会尝试重新创建表。还包括先前的迁移作为依赖项:
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)
]
This can be done fairly easily using migrations.SeparateDatabaseAndState
. Basically, we use a database operation to rename the table concurrently with two state operations to remove the model from one app’s history and create it in another’s.
Remove from old app
python manage.py makemigrations old_app --empty
In the migration:
class Migration(migrations.Migration):
dependencies = []
database_operations = [
migrations.AlterModelTable('TheModel', 'newapp_themodel')
]
state_operations = [
migrations.DeleteModel('TheModel')
]
operations = [
migrations.SeparateDatabaseAndState(
database_operations=database_operations,
state_operations=state_operations)
]
Add to new app
First, copy the model to the new app’s model.py, then:
python manage.py makemigrations new_app
This will generate a migration with a naive CreateModel
operation as the sole operation. Wrap that in a SeparateDatabaseAndState
operation such that we don’t try to recreate the table. Also include the prior migration as a dependency:
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)
]
回答 2
我遇到了同样的问题。
奥赞的回答对我有很大帮助,但不幸的是还不够。确实,我有几个ForeignKey链接到我想移动的模型。经过一番头痛之后,我发现了解决方案,因此决定发布该解决方案以解决人们的时间问题。
您还需要2个步骤:
- 在执行任何操作之前,请将所有
ForeignKey
链接更改TheModel
为Integerfield
。然后跑python manage.py makemigrations
- 完成Ozan的步骤后,请重新转换外键:放回
ForeignKey(TheModel)
而不是IntegerField()
。然后再次进行迁移(python manage.py makemigrations
)。然后,您可以迁移,它应该可以工作(python manage.py migrate
)
希望能帮助到你。当然,在尝试生产之前,请先在本地进行测试,以避免出现意外情况:)
I encountered the same problem.
Ozan’s answer helped me a lot but unfortunately was not enough. Indeed I had several ForeignKey linking to the model I wanted to move. After some headache I found the solution so decided to post it to solve people time.
You need 2 more steps:
- Before doing anything, change all your
ForeignKey
linking to TheModel
into Integerfield
. Then run python manage.py makemigrations
- After doing Ozan’s steps, re-convert your foreign keys: put back
ForeignKey(TheModel)
instead of IntegerField()
. Then make the migrations again (python manage.py makemigrations
). You can then migrate and it should work (python manage.py migrate
)
Hope it helps. Of course test it in local before trying in production to avoid bad suprises :)
回答 3
我是如何做到的(在PostgreSQL == 1.8上进行了测试,使用postgres,可能也是1.7)
情况
app1.YourModel
但您希望它转到:
app2.YourModel
- 将YourModel(代码)从app1复制到app2。
将此添加到app2.YourModel:
Class Meta:
db_table = 'app1_yourmodel'
$ python manage.py makemigrations app2
使用migrations.CreateModel()语句在app2中进行了新的迁移(例如0009_auto_something.py),将该语句移至app2的初始迁移(例如0001_initial.py)(就像它一直在那儿一样)。现在删除创建的迁移= 0009_auto_something.py
就像您执行操作一样,就像app2.YourModel一直存在一样,现在从迁移中删除app1.YourModel的存在。含义:注释掉CreateModel语句,以及之后使用的所有调整或数据迁移。
当然,必须在您的项目中将对app1.YourModel的每个引用都更改为app2.YourModel。另外,不要忘记迁移中所有可能的app1.YourModel外键都必须更改为app2.YourModel。
现在,如果您执行$ python manage.py迁移,则什么都没有改变,同样,当您执行$ python manage.py makemigrations时,也不会检测到新的东西。
现在画龙点睛:从app2.YourModel中删除Class Meta并执行$ python manage.py makemigrations app2 && python manage.py migration app2(如果您研究此迁移,您将看到类似以下的内容:)
migrations.AlterModelTable(
name='yourmodel',
table=None,
),
table = None,表示它将采用默认的表名,在这种情况下为app2_yourmodel。
- 完成,保存数据。
PS在迁移期间,它将看到content_type app1.yourmodel已被删除并且可以删除。您可以对此说是,但前提是您不使用它。如果您非常依赖它来使该内容类型的FK保持完整,则不要回答yes或no,而是手动进入该时间的db,并删除contentype app2.yourmodel,然后重命名contenttype app1。 yourmodel到app2.yourmodel,然后继续回答否。
How I did it (tested on Django==1.8, with postgres, so probably also 1.7)
Situation
app1.YourModel
but you want it to go to:
app2.YourModel
- Copy YourModel (the code) from app1 to app2.
add this to app2.YourModel:
Class Meta:
db_table = 'app1_yourmodel'
$ python manage.py makemigrations app2
A new migration (e.g. 0009_auto_something.py) is made in app2 with a migrations.CreateModel() statement, move this statement to the initial migration of app2 (e.g. 0001_initial.py) (it will be just like it always have been there). And now remove the created migration = 0009_auto_something.py
Just as you act, like app2.YourModel always has been there, now remove the existence of app1.YourModel from your migrations. Meaning: comment out the CreateModel statements, and every adjustment or datamigration you used after that.
And of course, every reference to app1.YourModel has to be changed to app2.YourModel through your project. Also, don’t forget that all possible foreign keys to app1.YourModel in migrations have to be changed to app2.YourModel
Now if you do $ python manage.py migrate, nothing has changed, also when you do $ python manage.py makemigrations, nothing new has been detected.
Now the finishing touch: remove the Class Meta from app2.YourModel and do $ python manage.py makemigrations app2 && python manage.py migrate app2 (if you look into this migration you’ll see something like this:)
migrations.AlterModelTable(
name='yourmodel',
table=None,
),
table=None, means it will take the default table-name, which in this case will be app2_yourmodel.
- DONE, with data saved.
P.S during the migration it will see that that content_type app1.yourmodel has been removed and can be deleted. You can say yes to that but only if you don’t use it. In case you heavily depend on it to have FKs to that content-type be intact, don’t answer yes or no yet, but go into the db that time manually, and remove the contentype app2.yourmodel, and rename the contenttype app1.yourmodel to app2.yourmodel, and then continue by answering no.
回答 4
我会感到紧张的手工编码迁移(这是Ozan的回答所要求的),因此以下内容结合了Ozan和Michael的策略以最大程度地减少所需的手工编码量:
- 在移动任何模型之前,请通过运行确保使用干净的基线
makemigrations
。
- 将模型的代码从
app1
移至app2
按照@Michael的建议,我们使用db_table
“新”模型上的Meta选项将新模型指向旧数据库表:
class Meta:
db_table = 'app1_yourmodel'
运行makemigrations
。这将CreateModel
在app2
和DeleteModel
中生成app1
。从技术上讲,这些迁移将引用完全相同的表,并且将删除(包括所有数据)并重新创建表。
实际上,我们不希望(或不需要)对表做任何事情。我们只需要Django相信所做的更改即可。根据@Ozan的回答,中的state_operations
标志将SeparateDatabaseAndState
执行此操作。因此,我们使用来将所有migrations
条目包装在两个迁移文件中SeparateDatabaseAndState(state_operations=[...])
。例如,
operations = [
...
migrations.DeleteModel(
name='YourModel',
),
...
]
变成
operations = [
migrations.SeparateDatabaseAndState(state_operations=[
...
migrations.DeleteModel(
name='YourModel',
),
...
])
]
您还需要确保新的“虚拟” CreateModel
迁移取决于实际创建或更改原始表的任何迁移。例如,如果您的新迁移是app2.migrations.0004_auto_<date>
(针对Create
)和app1.migrations.0007_auto_<date>
(针对Delete
),则最简单的操作是:
- 打开
app1.migrations.0007_auto_<date>
并复制其app1
依赖项(例如 ('app1', '0006...'),
)。这是“立即优先”的迁移app1
,应该包括对所有实际模型构建逻辑的依赖。
- 打开
app2.migrations.0004_auto_<date>
并将刚刚复制的依赖项添加到其dependencies
列表中。
如果您ForeignKey
与要移动的模型有关系,则上述方法可能无效。发生这种情况是因为:
- 不会自动为
ForeignKey
更改创建依赖关系
- 我们不想包装这些
ForeignKey
更改,state_operations
因此我们需要确保它们与表操作是分开的。
注意:Django 2.2添加了一个警告(models.E028
),它破坏了此方法。您也许可以解决该问题,managed=False
但我尚未对其进行测试。
“最小”操作集因情况而异,但是以下过程应适用于大多数/所有ForeignKey
迁移:
- 从
app1
到复制模型app2
,设置db_table
,但不要更改任何FK引用。
- 运行
makemigrations
并包装所有app2
迁移state_operations
(请参见上文)
- 如上所述,将依赖项添加
app2
CreateTable
到最新的app1
迁移中
- 将所有FK参考指向新模型。如果您不使用字符串引用,请将旧模型移到
models.py
(不要删除它)的底部,这样它就不会与导入的类竞争。
运行,makemigrations
但不要包装任何内容state_operations
(实际上应该发生FK更改)。将所有ForeignKey
迁移(即AlterField
)中的依赖项添加到其中的CreateTable
迁移中app2
(下一步将需要此列表,因此请对其进行跟踪)。例如:
- 找到包含迁移
CreateModel
如app2.migrations.0002_auto_<date>
和复制迁移的名称。
查找所有具有该模型的ForeignKey的迁移(例如,通过搜索app2.YourModel
找到类似以下的迁移:
class Migration(migrations.Migration):
dependencies = [
('otherapp', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='relatedmodel',
name='fieldname',
field=models.ForeignKey(... to='app2.YourModel'),
),
]
将CreateModel
迁移作为依赖项添加:
class Migration(migrations.Migration):
dependencies = [
('otherapp', '0001_initial'),
('app2', '0002_auto_<date>'),
]
从中删除模型 app1
- 运行迁移
makemigrations
并将其包装在app1
中state_operations
。
- 为上一步的所有
ForeignKey
迁移(即AlterField
)添加一个依赖项(可能包括app1
和中的迁移app2
)。
- 当我构建这些迁移时,
DeleteTable
已经依赖于AlterField
迁移了,因此我不需要手动执行它(即Alter
before Delete
)。
在这一点上,Django是个不错的选择。新模型指向旧表,并且Django的迁移使它确信所有内容都已适当地重新放置。最大的警告(来自@Michael的答案)是ContentType
为新模型创建了一个新模型。如果您链接(例如通过ForeignKey
)到内容类型,则需要创建一个迁移来更新ContentType
表。
我想自己清理一下(元选项和表名),所以我使用了以下过程(来自@Michael):
- 删除
db_table
元条目
makemigrations
再次运行以生成数据库重命名
- 编辑此最后的迁移,并确保它取决于
DeleteTable
迁移。似乎没有必要,因为它应该Delete
纯粹是逻辑上的,但是app1_yourmodel
如果我不这样做,就会遇到错误(例如,不存在)。
I get nervous hand-coding migrations (as is required by Ozan’s answer) so the following combines Ozan’s and Michael’s strategies to minimize the amount of hand-coding required:
- Before moving any models, make sure you’re working with a clean baseline by running
makemigrations
.
- Move the code for the Model from
app1
to app2
As recommended by @Michael, we point the new model to the old database table using the db_table
Meta option on the “new” model:
class Meta:
db_table = 'app1_yourmodel'
Run makemigrations
. This will generate CreateModel
in app2
and DeleteModel
in app1
. Technically, these migrations refer to the exact same table and would remove (including all data) and re-create the table.
In reality, we don’t want (or need) to do anything to the table. We just need Django to believe that the change has been made. Per @Ozan’s answer, the state_operations
flag in SeparateDatabaseAndState
does this. So we wrap all of the migrations
entries IN BOTH MIGRATIONS FILES with SeparateDatabaseAndState(state_operations=[...])
. For example,
operations = [
...
migrations.DeleteModel(
name='YourModel',
),
...
]
becomes
operations = [
migrations.SeparateDatabaseAndState(state_operations=[
...
migrations.DeleteModel(
name='YourModel',
),
...
])
]
You also need to make sure the new “virtual” CreateModel
migration depends on any migration that actually created or altered the original table. For example, if your new migrations are app2.migrations.0004_auto_<date>
(for the Create
) and app1.migrations.0007_auto_<date>
(for the Delete
), the simplest thing to do is:
- Open
app1.migrations.0007_auto_<date>
and copy its app1
dependency (e.g. ('app1', '0006...'),
). This is the “immediately prior” migration in app1
and should include dependencies on all of the actual model building logic.
- Open
app2.migrations.0004_auto_<date>
and add the dependency you just copied to its dependencies
list.
If you have ForeignKey
relationship(s) to the model you’re moving, the above may not work. This happens because:
- Dependencies are not automatically created for the
ForeignKey
changes
- We do not want to wrap the
ForeignKey
changes in state_operations
so we need to ensure they are separate from the table operations.
NOTE: Django 2.2 added a warning (models.E028
) that breaks this method. You may be able to work around it with managed=False
but I have not tested it.
The “minimum” set of operations differ depending on the situation, but the following procedure should work for most/all ForeignKey
migrations:
- COPY the model from
app1
to app2
, set db_table
, but DON’T change any FK references.
- Run
makemigrations
and wrap all app2
migration in state_operations
(see above)
- As above, add a dependency in the
app2
CreateTable
to the latest app1
migration
- Point all of the FK references to the new model. If you aren’t using string references, move the old model to the bottom of
models.py
(DON’T remove it) so it doesn’t compete with the imported class.
Run makemigrations
but DON’T wrap anything in state_operations
(the FK changes should actually happen). Add a dependency in all the ForeignKey
migrations (i.e. AlterField
) to the CreateTable
migration in app2
(you’ll need this list for the next step so keep track of them). For example:
- Find the migration that includes the
CreateModel
e.g. app2.migrations.0002_auto_<date>
and copy the name of that migration.
Find all migrations that have a ForeignKey to that model (e.g. by searching app2.YourModel
to find migrations like:
class Migration(migrations.Migration):
dependencies = [
('otherapp', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='relatedmodel',
name='fieldname',
field=models.ForeignKey(... to='app2.YourModel'),
),
]
Add the CreateModel
migration as as a dependency:
class Migration(migrations.Migration):
dependencies = [
('otherapp', '0001_initial'),
('app2', '0002_auto_<date>'),
]
Remove the models from app1
- Run
makemigrations
and wrap the app1
migration in state_operations
.
- Add a dependency to all of the
ForeignKey
migrations (i.e. AlterField
) from the previous step (may include migrations in app1
and app2
).
- When I built these migrations, the
DeleteTable
already depended on the AlterField
migrations so I didn’t need to manually enforce it (i.e. Alter
before Delete
).
At this point, Django is good to go. The new model points to the old table and Django’s migrations have convinced it that everything has been relocated appropriately. The big caveat (from @Michael’s answer) is that a new ContentType
is created for the new model. If you link (e.g. by ForeignKey
) to content types, you’ll need to create a migration to update the ContentType
table.
I wanted to cleanup after myself (Meta options and table names) so I used the following procedure (from @Michael):
- Remove the
db_table
Meta entry
- Run
makemigrations
again to generate the database rename
- Edit this last migration and make sure it depends on the
DeleteTable
migration. It doesn’t seem like it should be necessary as the Delete
should be purely logical, but I’ve run into errors (e.g. app1_yourmodel
doesn’t exist) if I don’t.
回答 5
如果数据不是很大或太复杂,但仍然很重要,则另一种骇人听闻的选择是:
Another hacky alternative if the data is not big or too complicated, but still important to maintain, is to:
- Get data fixtures using manage.py dumpdata
- Proceed to model changes and migrations properly, without relating the changes
- Global replace the fixtures from the old model and app names to the new
- Load data using manage.py loaddata
回答 6
从我在https://stackoverflow.com/a/47392970/8971048的回答中复制
万一您需要移动模型而又无权访问该应用程序(或者您不想访问该应用程序),则可以创建一个新的Operation并考虑仅在迁移的模型不具有该模型的情况下创建一个新模型存在。
在此示例中,我将“ MyModel”从old_app传递到myapp。
class MigrateOrCreateTable(migrations.CreateModel):
def __init__(self, source_table, dst_table, *args, **kwargs):
super(MigrateOrCreateTable, self).__init__(*args, **kwargs)
self.source_table = source_table
self.dst_table = dst_table
def database_forwards(self, app_label, schema_editor, from_state, to_state):
table_exists = self.source_table in schema_editor.connection.introspection.table_names()
if table_exists:
with schema_editor.connection.cursor() as cursor:
cursor.execute("RENAME TABLE {} TO {};".format(self.source_table, self.dst_table))
else:
return super(MigrateOrCreateTable, self).database_forwards(app_label, schema_editor, from_state, to_state)
class Migration(migrations.Migration):
dependencies = [
('myapp', '0002_some_migration'),
]
operations = [
MigrateOrCreateTable(
source_table='old_app_mymodel',
dst_table='myapp_mymodel',
name='MyModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=18))
],
),
]
Copied from my answer at https://stackoverflow.com/a/47392970/8971048
In case you need to move the model and you don’t have access to the app anymore (or you don’t want the access), you can create a new Operation and consider to create a new model only if the migrated model does not exist.
In this example I am passing ‘MyModel’ from old_app to myapp.
class MigrateOrCreateTable(migrations.CreateModel):
def __init__(self, source_table, dst_table, *args, **kwargs):
super(MigrateOrCreateTable, self).__init__(*args, **kwargs)
self.source_table = source_table
self.dst_table = dst_table
def database_forwards(self, app_label, schema_editor, from_state, to_state):
table_exists = self.source_table in schema_editor.connection.introspection.table_names()
if table_exists:
with schema_editor.connection.cursor() as cursor:
cursor.execute("RENAME TABLE {} TO {};".format(self.source_table, self.dst_table))
else:
return super(MigrateOrCreateTable, self).database_forwards(app_label, schema_editor, from_state, to_state)
class Migration(migrations.Migration):
dependencies = [
('myapp', '0002_some_migration'),
]
operations = [
MigrateOrCreateTable(
source_table='old_app_mymodel',
dst_table='myapp_mymodel',
name='MyModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=18))
],
),
]
回答 7
这是经过粗略测试的,所以不要忘记备份数据库!!!
例如,有两个应用程序:src_app
和dst_app
,我们希望将模型MoveMe
从src_app
移至dst_app
。
为两个应用程序创建空迁移:
python manage.py makemigrations --empty src_app
python manage.py makemigrations --empty dst_app
让我们假设,新的迁移是XXX1_src_app_new
和XXX1_dst_app_new
,之前的主要迁移是XXX0_src_app_old
和XXX0_dst_app_old
。
添加一个操作,该操作重命名MoveMe
模型的表,并将其在ProjectState中的app_label重命名为XXX1_dst_app_new
。不要忘记增加对XXX0_src_app_old
迁移的依赖。产生的XXX1_dst_app_new
迁移是:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
# this operations is almost the same as RenameModel
# https://github.com/django/django/blob/1.7/django/db/migrations/operations/models.py#L104
class MoveModelFromOtherApp(migrations.operations.base.Operation):
def __init__(self, name, old_app_label):
self.name = name
self.old_app_label = old_app_label
def state_forwards(self, app_label, state):
# Get all of the related objects we need to repoint
apps = state.render(skip_cache=True)
model = apps.get_model(self.old_app_label, self.name)
related_objects = model._meta.get_all_related_objects()
related_m2m_objects = model._meta.get_all_related_many_to_many_objects()
# Rename the model
state.models[app_label, self.name.lower()] = state.models.pop(
(self.old_app_label, self.name.lower())
)
state.models[app_label, self.name.lower()].app_label = app_label
for model_state in state.models.values():
try:
i = model_state.bases.index("%s.%s" % (self.old_app_label, self.name.lower()))
model_state.bases = model_state.bases[:i] + ("%s.%s" % (app_label, self.name.lower()),) + model_state.bases[i+1:]
except ValueError:
pass
# Repoint the FKs and M2Ms pointing to us
for related_object in (related_objects + related_m2m_objects):
# Use the new related key for self referential related objects.
if related_object.model == model:
related_key = (app_label, self.name.lower())
else:
related_key = (
related_object.model._meta.app_label,
related_object.model._meta.object_name.lower(),
)
new_fields = []
for name, field in state.models[related_key].fields:
if name == related_object.field.name:
field = field.clone()
field.rel.to = "%s.%s" % (app_label, self.name)
new_fields.append((name, field))
state.models[related_key].fields = new_fields
def database_forwards(self, app_label, schema_editor, from_state, to_state):
old_apps = from_state.render()
new_apps = to_state.render()
old_model = old_apps.get_model(self.old_app_label, self.name)
new_model = new_apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
# Move the main table
schema_editor.alter_db_table(
new_model,
old_model._meta.db_table,
new_model._meta.db_table,
)
# Alter the fields pointing to us
related_objects = old_model._meta.get_all_related_objects()
related_m2m_objects = old_model._meta.get_all_related_many_to_many_objects()
for related_object in (related_objects + related_m2m_objects):
if related_object.model == old_model:
model = new_model
related_key = (app_label, self.name.lower())
else:
model = related_object.model
related_key = (
related_object.model._meta.app_label,
related_object.model._meta.object_name.lower(),
)
to_field = new_apps.get_model(
*related_key
)._meta.get_field_by_name(related_object.field.name)[0]
schema_editor.alter_field(
model,
related_object.field,
to_field,
)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
self.old_app_label, app_label = app_label, self.old_app_label
self.database_forwards(app_label, schema_editor, from_state, to_state)
app_label, self.old_app_label = self.old_app_label, app_label
def describe(self):
return "Move %s from %s" % (self.name, self.old_app_label)
class Migration(migrations.Migration):
dependencies = [
('dst_app', 'XXX0_dst_app_old'),
('src_app', 'XXX0_src_app_old'),
]
operations = [
MoveModelFromOtherApp('MoveMe', 'src_app'),
]
将依赖项添加XXX1_dst_app_new
到XXX1_src_app_new
。XXX1_src_app_new
是No-op迁移,可确保src_app
在之后执行将来的迁移XXX1_dst_app_new
。
移动MoveMe
从src_app/models.py
到dst_app/models.py
。然后运行:
python manage.py migrate
就这样!
This is tested roughly, so do not forget to backup your DB!!!
For example, there are two apps: src_app
and dst_app
, we want to move model MoveMe
from src_app
to dst_app
.
Create empty migrations for both apps:
python manage.py makemigrations --empty src_app
python manage.py makemigrations --empty dst_app
Let’s assume, that new migrations are XXX1_src_app_new
and XXX1_dst_app_new
, previuos top migrations are XXX0_src_app_old
and XXX0_dst_app_old
.
Add an operation that renames table for MoveMe
model and renames its app_label in ProjectState to XXX1_dst_app_new
. Do not forget to add dependency on XXX0_src_app_old
migration. The resulting XXX1_dst_app_new
migration is:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
# this operations is almost the same as RenameModel
# https://github.com/django/django/blob/1.7/django/db/migrations/operations/models.py#L104
class MoveModelFromOtherApp(migrations.operations.base.Operation):
def __init__(self, name, old_app_label):
self.name = name
self.old_app_label = old_app_label
def state_forwards(self, app_label, state):
# Get all of the related objects we need to repoint
apps = state.render(skip_cache=True)
model = apps.get_model(self.old_app_label, self.name)
related_objects = model._meta.get_all_related_objects()
related_m2m_objects = model._meta.get_all_related_many_to_many_objects()
# Rename the model
state.models[app_label, self.name.lower()] = state.models.pop(
(self.old_app_label, self.name.lower())
)
state.models[app_label, self.name.lower()].app_label = app_label
for model_state in state.models.values():
try:
i = model_state.bases.index("%s.%s" % (self.old_app_label, self.name.lower()))
model_state.bases = model_state.bases[:i] + ("%s.%s" % (app_label, self.name.lower()),) + model_state.bases[i+1:]
except ValueError:
pass
# Repoint the FKs and M2Ms pointing to us
for related_object in (related_objects + related_m2m_objects):
# Use the new related key for self referential related objects.
if related_object.model == model:
related_key = (app_label, self.name.lower())
else:
related_key = (
related_object.model._meta.app_label,
related_object.model._meta.object_name.lower(),
)
new_fields = []
for name, field in state.models[related_key].fields:
if name == related_object.field.name:
field = field.clone()
field.rel.to = "%s.%s" % (app_label, self.name)
new_fields.append((name, field))
state.models[related_key].fields = new_fields
def database_forwards(self, app_label, schema_editor, from_state, to_state):
old_apps = from_state.render()
new_apps = to_state.render()
old_model = old_apps.get_model(self.old_app_label, self.name)
new_model = new_apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
# Move the main table
schema_editor.alter_db_table(
new_model,
old_model._meta.db_table,
new_model._meta.db_table,
)
# Alter the fields pointing to us
related_objects = old_model._meta.get_all_related_objects()
related_m2m_objects = old_model._meta.get_all_related_many_to_many_objects()
for related_object in (related_objects + related_m2m_objects):
if related_object.model == old_model:
model = new_model
related_key = (app_label, self.name.lower())
else:
model = related_object.model
related_key = (
related_object.model._meta.app_label,
related_object.model._meta.object_name.lower(),
)
to_field = new_apps.get_model(
*related_key
)._meta.get_field_by_name(related_object.field.name)[0]
schema_editor.alter_field(
model,
related_object.field,
to_field,
)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
self.old_app_label, app_label = app_label, self.old_app_label
self.database_forwards(app_label, schema_editor, from_state, to_state)
app_label, self.old_app_label = self.old_app_label, app_label
def describe(self):
return "Move %s from %s" % (self.name, self.old_app_label)
class Migration(migrations.Migration):
dependencies = [
('dst_app', 'XXX0_dst_app_old'),
('src_app', 'XXX0_src_app_old'),
]
operations = [
MoveModelFromOtherApp('MoveMe', 'src_app'),
]
Add dependency on XXX1_dst_app_new
to XXX1_src_app_new
. XXX1_src_app_new
is no-op migration that is needed to make sure that future src_app
migrations will be executed after XXX1_dst_app_new
.
Move MoveMe
from src_app/models.py
to dst_app/models.py
. Then run:
python manage.py migrate
That’s all!
回答 8
You can try the following (untested):
- move the model from
src_app
to dest_app
- migrate
dest_app
; make sure the schema migration depends on the latest src_app
migration (https://docs.djangoproject.com/en/dev/topics/migrations/#migration-files)
- add a data migration to
dest_app
, that copies all data from src_app
- migrate
src_app
; make sure the schema migration depends on the latest (data) migration of dest_app
— that is: the migration of step 3
Note that you will be copying the whole table, instead of moving it, but that way both apps don’t have to touch a table that belongs to the other app, which I think is more important.
回答 9
假设您要将模型TheModel从app_a移至app_b。
另一种解决方案是手动更改现有迁移。这样做的想法是,每次在app_a的迁移过程中看到更改TheModel的操作时,都会将该操作复制到app_b的初始迁移的末尾。并且每次在app_a的迁移中看到引用’app_a.TheModel’时,都将其更改为’app_b.TheModel’。
我只是对一个现有项目执行此操作,在该项目中我想将某个模型提取到可重用的应用程序中。程序进行得很顺利。我想如果有从app_b到app_a的引用,事情会更加困难。另外,我为模型手动定义了Meta.db_table,这可能有所帮助。
值得注意的是,您最终会更改迁移历史记录。即使您的数据库已应用原始迁移,也没关系。如果原始迁移和重写迁移最终都具有相同的数据库模式,则这种重写应该可以。
Lets say you are moving model TheModel from app_a to app_b.
An alternate solution is to alter the existing migrations by hand. The idea is that each time you see an operation altering TheModel in app_a’s migrations, you copy that operation to the end of app_b’s initial migration. And each time you see a reference ‘app_a.TheModel’ in app_a’s migrations, you change it to ‘app_b.TheModel’.
I just did this for an existing project, where I wanted to extract a certain model to an reusable app. The procedure went smoothly. I guess things would be much harder if there were references from app_b to app_a. Also, I had a manually defined Meta.db_table for my model which might have helped.
Notably you will end up with altered migration history. This doesn’t matter, even if you have a database with the original migrations applied. If both the original and the rewritten migrations end up with the same database schema, then such rewrite should be OK.
回答 10
- 将旧模型的名称更改为“ model_name_old”
- 移民
- 制作名为“ model_name_new”的新模型,并在相关模型上具有相同的关系(例如,用户模型现在具有user.blog_old和user.blog_new)
- 移民
- 编写自定义迁移,将所有数据迁移到新模型表
- 通过在运行迁移之前和之后将备份与新的数据库副本进行比较来测试这些迁移的难处
- 当一切都令人满意时,删除旧模型
- 移民
- 将新模型更改为正确的名称’model_name_new’->’model_name’
- 在登台服务器上测试整个迁移过程
- 将您的生产站点关闭几分钟,以便在不干扰用户的情况下运行所有迁移
对每个需要移动的模型分别执行此操作。我不建议通过更改为整数然后再返回外键来执行其他答案所说的事情,在迁移之后,新的外键可能会有所不同,并且行的ID可能会有所不同,并且我不想冒任何风险切换回外键时ID不匹配的问题。
- change the names of old models to ‘model_name_old’
- makemigrations
- make new models named ‘model_name_new’ with identical relationships on the related models
(eg. user model now has user.blog_old and user.blog_new)
- makemigrations
- write a custom migration that migrates all the data to the new model tables
- test the hell out of these migrations by comparing backups with new db copies before and after running the migrations
- when all is satisfactory, delete the old models
- makemigrations
- change the new models to the correct name ‘model_name_new’ -> ‘model_name’
- test the whole slew of migrations on a staging server
- take your production site down for a few minutes in order to run all migrations without users interfering
Do this individually for each model that needs to be moved.
I wouldn’t suggest doing what the other answer says by changing to integers and back to foreign keys
There is a chance that new foreign keys will be different and rows may have different IDs after the migrations and I didn’t want to run any risk of mismatching ids when switching back to foreign keys.