【问题标题】:Stale content type prompt deleting all model instances after renaming django model with permissions使用权限重命名 django 模型后,过时的内容类型提示删除所有模型实例
【发布时间】: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',
    ),  
] 

【问题讨论】:

    标签: django database-migration


    【解决方案1】:

    如果您想重命名您的表格,请查看RenameModel。是的,Django 没有检测到重命名的模型。所以,你需要手动添加它。

    【讨论】:

    • Django 确实自动检测了重命名的模型。请参阅我对已创建迁移的编辑。即使它使用 RenameModel,我仍然有这个问题。
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 2020-01-01
    • 2015-02-28
    • 2011-03-26
    • 2017-01-21
    • 1970-01-01
    • 2013-01-07
    • 2019-03-24
    相关资源
    最近更新 更多