当我在我的某个项目应用程序上执行迁移时,出现以下错误:
ValueError:模型未处理的待处理操作: common.shipmentaddress(由字段引用:catalog.Fulfillment.address)
Django 1.9,python 2.7.10
我一直在寻找循环导入,但我不认为这是它
这些是模型:
class ShipmentAddress(models.Model):
recipient_first_name = models.CharField(max_length=50, null=True, blank=True)
recipient_last_name = models.CharField(max_length=50, null=True, blank=True)
street_name = models.CharField(max_length=50)
state = models.ForeignKey(State)
postal_code = models.IntegerField(default=0)
city = models.CharField(max_length=50)
class Meta:
db_table = 'shipment_address'
class Fulfillment(models.Model):
address = models.ForeignKey(ShipmentAddress)
inventory_items = models.ManyToManyField(Item_With_Size, through='Inventory_Item')
class Meta:
verbose_name = 'fulfilment'
verbose_name_plural = 'fulfilments'
db_table = 'fulfilment'
迁移看起来像这样:
class Migration(migrations.Migration):
dependencies = [
('catalog', '0009_auto_20151130_1118'),
]
operations = [
migrations.AlterField(
model_name='fulfillment',
name='address',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='common.ShipmentAddress'),
),
]
class Migration(migrations.Migration):
dependencies = [
('common', '0005_shipmentaddress'),
]
operations = [
migrations.RenameField(
model_name='shipmentaddress',
old_name='recipient_name',
new_name='recipient_first_name',
),
migrations.AddField(
model_name='shipmentaddress',
name='recipient_last_name',
field=models.CharField(blank=True, max_length=50, null=True),
),
]
答案 0 :(得分:4)
好的,我明白了!
似乎迁移过程超过了我以前的所有迁移过程,而不仅仅是最后一次迁移...在之前的一次迁移中,出现了导致该问题的错误的外键指针
我修复了旧的迁移,就是这样!
答案 1 :(得分:3)
对于那些重命名Django ForeignKey
中引用的模型的人来说,我发现这个问题的另一个解决方案是在以后的迁移中重新定义该字段。这样可以避免编辑现有迁移。
如果您在第一次迁移中有以下操作(自动添加):
migrations.AddField(
model_name='my_model',
name='my_fk',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='my_app.old_model_name'),
)
在将old_model_name
重命名为new_model_name
的迁移中,手动添加以下操作:
migrations.AlterField(
model_name='my_model',
name='my_fk',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='my_app.new_model_name'),
)
两者之间的差异是调用AlterField
而不是AddField
,to
引用ForeignKey
字段中的新模型名称。
这是在Django 1.9.12上测试的。