【发布时间】:2015-08-25 00:20:11
【问题描述】:
我有两个模型,称为 CombinedProduct 和 CombinedProductPrice,我分别将它们重命名为 Set 和 SetPrice。我通过在 models.py 文件中更改它们的模型名称并替换所有出现的它来做到这一点。这还包括将另一个模型中的外键字段从 combine_product 重命名为 set(指向 CombinedProduct)。
当运行 makemigrations django 正确检测到重命名并询问我是否已重命名所有这三个内容时,我都按“是”。但是,在运行“迁移”时,在应用了一些东西后,我被问到:
The following content types are stale and need to be deleted:
product | combinedproduct
product | combinedproductprice
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
我备份了我的数据并输入了“是”,这删除了 Set(以前是 CombinedProduct)和 SetPrice(以前是 CombinedProductPrice)的所有实例。如果我回滚并勾选否,那么每次迁移时都会出现这个问题。
这很奇怪,因为我没有在任何地方使用任何 django ContentType 框架。在检查哪些字段指向 ContentType 但是我看到 auth.permission 指向它,并且我使用这些模型的权限。所以也许删除从指向旧模型名称的旧权限级联,这反过来又会删除我的实例?如果是这样,我该如何防止这种情况发生?
这是生成的迁移:
operations = [
migrations.RenameModel(
old_name='CombinedProduct',
new_name='Set',
),
migrations.RenameModel(
old_name='CombinedProductPrice',
new_name='SetPrice',
),
migrations.AlterModelOptions(
name='setprice',
options={'ordering': ('set', 'vendor', 'price'), 'verbose_name': 'Set price', 'verbose_name_plural': 'Set prices'},
),
migrations.RenameField(
model_name='setprice',
old_name='combined_product',
new_name='set',
),
]
【问题讨论】: