迁移前从模型访问Django模型字段时出现问题
我正在将字段迁移前从模型访问Django模型字段时出现问题,django,django-models,django-migrations,Django,Django Models,Django Migrations,我正在将字段some_字段从Model_A移动到一个新的Model_B,具有一对一关系。在删除Model\u A中的此字段之前,我想将值从(历史)Model\u A复制到新创建的Model\u B。问题是我无法在迁移时检索该字段,因为Model\u A不再包含some\u字段 这是我尝试运行自定义迁移时收到的错误消息: AttributeError: 'Model_A' object has no attribute 'some_field' 更改前的模型: class Model_A:
some_字段
从Model_A
移动到一个新的Model_B
,具有一对一关系。在删除Model\u A
中的此字段之前,我想将值从(历史)Model\u A
复制到新创建的Model\u B
。问题是我无法在迁移时检索该字段,因为Model\u A
不再包含some\u字段
这是我尝试运行自定义迁移时收到的错误消息:
AttributeError: 'Model_A' object has no attribute 'some_field'
更改前的模型:
class Model_A:
some_field = models.BooleanField(default=False)
some_other_field = models.BooleanField(default=False)
更改后的型号:
class Model_A:
some_other_field = models.BooleanField(default=False)
class Model_B:
model_a = models.OneToOneField(Model_A, related_name='extension')
some_field = models.BooleanField(default=False)
迁移:
class Migration(migrations.Migration):
dependencies = [
('my_app', '0001_initial'),
]
def forwards_func(apps, schema_editor):
# This is where I try to get the "historical" Model_A
Model_A = apps2.get_model("my_app", "Model_A")
# And this is where I intend to copy the some_field values
for model_A_instance in Model_A.objects.all():
b = Model_B(model_a=model_A_instance)
# b gets created correctly, but the following step fails
b.some_field = modelA_instance.some_field
b.save()
operations = [
migrations.CreateModel(
name='Model_B',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('some_field', models.BooleanField(default=False)),
('model_a', models.OneToOneField(related_name='extension', to='my_app.Model_A')),
],
options={
},
bases=(models.Model,),
),
migrations.RunPython(forwards_func),
migrations.RemoveField(
model_name='model_a',
name='some_field',
),
]
我非常清楚,我必须以某种方式获得Model\u a
的“历史”表示(当前在数据库中的表示),但我认为这就是apps2.get\u Model(“我的应用程序”,“Model\u a”)
部分的目的
关于如何实现这一点,有什么意见吗?或者我应该将迁移分为两部分,第一部分创建
Model\u B
+复制some\u字段
值,第二部分从Model\u A
中删除some\u字段?是的,您需要有Model\u A
的历史表示,并且很容易检索它。这就是apps
传递到RunPython
migration调用的函数的目的,那么为什么要在这里使用一些apps2
insdead应用呢?此外,您应该从与Model_A
相同的apps
实例获取Model_B
。您的迁移应该如下所示:
class Migration(migrations.Migration):
dependencies = [
('my_app', '0001_initial'),
]
def forwards_func(apps, schema_editor):
# This is where I try to get the "historical" Model_A
# here is change - using apps passed into forwards_func by RunPython instead of apps2
Model_A = apps.get_model("my_app", "Model_A")
Model_B = apps.get_model("my_app", "Model_B")
# And this is where I intend to copy the some_field values
for model_A_instance in Model_A.objects.all():
b = Model_B(model_a=model_A_instance)
b.some_field = modelA_instance.some_field
b.save()
operations = [
migrations.CreateModel(
name='Model_B',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('some_field', models.BooleanField(default=False)),
('model_a', models.OneToOneField(related_name='extension', to='my_app.Model_A')),
],
options={
},
bases=(models.Model,),
),
migrations.RunPython(forwards_func),
migrations.RemoveField(
model_name='model_a',
name='some_field',
),
]
你为什么要使用apps2?它是什么?我似乎忘记了将以前的导入尝试改回:从django.apps将应用作为apps2导入。不幸的是,删除该导入并运行应用。get_model()并不能解决问题。它给了我以下错误:ValueError:无法分配“”:“Model_B.Model_a”必须是“Model_a”实例。您的
Model_B
应该使用与Model_a
相同的应用程序获取,请参阅更新的答案。GwynBleidD,您应该获得奖牌!当我以与A型车相同的方式拿到新车型时,一切都进行得很顺利。不知何故,我认为可以将它们混合在一起。显然,我错了!:)在迁移过程中,决不能从models.py
或其他源导入模型。始终依赖传递到自定义迁移函数中的应用程序。