无法在 Django Admin 中删除 — 对象将 ITSELF 称为受保护对象

Impossible to delete in Django Admin — object refers to ITSELF as a protected object

在 Django Admin 中,我有一个名为 Modules 的模型。

当我尝试删除模块对象时,出现以下错误:

Deleting the module 'FR menu' would require deleting
the following protected related objects:

Module: FR menu

我理解如果受保护的对象是不同的模型为什么会发生这种情况,但在这种情况下,警告指的是它本身

我什至不知道从哪里开始调试...也许是因为旧的迁移? . 此模型是 称为前缀 的模型的外键模型 — 这是 模块模型声明 :


positions = ('absolute', 'floating', 'none',)
corners = ('top left', 'top right', 'bottom left', 'bottom right',)

class Module(models.Model):

    name = models.CharField(max_length=200, default='')
    active = models.BooleanField(default=True, verbose_name='active',)
    display_order = models.PositiveSmallIntegerField(default=0, verbose_name='display order')
    sort1 = models.CharField(max_length=100, default='', verbose_name='main category', blank=True,)
    sort2 = models.CharField(max_length=100, default='', verbose_name='sub category', blank=True,)
    css_id = models.CharField(max_length=200, default='', verbose_name='object ID', blank=True,)
    filename = models.CharField(max_length=200, default='', blank=True, verbose_name='SVG file (optional)',)
    notes = RichTextField(default='')

    cache_reset   = models.BooleanField(default=False, verbose_name='delete cache (or visit example.com/c)',)

    position = models.CharField(max_length=255, default='absolute', choices=Choices(*positions), verbose_name='placement')
    corner = models.CharField(max_length=255, default='top left', choices=Choices(*corners), verbose_name='relative to')
    horz_offset = models.FloatField(default=0, verbose_name='horizontal offset (px)',)
    vert_offset = models.FloatField(default=0, verbose_name='vertical offset (px)',)

    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.name
    class Meta:
        ordering = ['-active', 'display_order', 'name', 'sort1', 'sort2',]
        verbose_name_plural = "2.2 · Modules"

class ModuleScripts(models.Model):
    module = models.ForeignKey(Module, on_delete=models.CASCADE)
    type = models.CharField(max_length=255, default='', choices=Choices(*script_types), verbose_name='type')
    name = models.CharField(max_length=200, default='')
    content = models.TextField(max_length=50000, default='', verbose_name='content',)
    order = models.IntegerField(default=0, verbose_name='load order')
    active = models.BooleanField(default=True, verbose_name='active',)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = "extra script"
        verbose_name_plural = "extra scripts"
        ordering = ["order"]

这里是前缀模型声明,其中包含模块作为外键:

class Prefix(models.Model):
    display_order = models.PositiveSmallIntegerField(default=0, verbose_name='display order')
    path = models.CharField(max_length=2, default='', verbose_name='code',)
    default = models.CharField(max_length=20, default='', verbose_name='default page')
    responsive = models.ForeignKey(Responsive, default=0, on_delete=models.PROTECT, verbose_name='screen',)
    language = models.ForeignKey(Language, default=0, on_delete=models.PROTECT, )
    module = models.ManyToManyField(Module, through='PrefixModules')
    def __str__(self):
        return self.path
    class Meta:
        verbose_name = "Combination Code"
        verbose_name_plural = "1.4 · Combination Codes"
        ordering = ['display_order']

class PrefixModules(models.Model):
    module = models.ForeignKey(Module, on_delete=models.PROTECT)
    prefix = models.ForeignKey(Prefix, on_delete=models.PROTECT)
    zindex = models.IntegerField(default=0, verbose_name='z index')
    active = models.BooleanField(default=True, verbose_name='active',)
    def __str__(self):
        return self.module.name
    class Meta:
        verbose_name = "module"
        verbose_name_plural = "modules"
        ordering = ["zindex"]

Module 似乎是指基于其 verbose_namePrefixModules,因此该错误很可能是指在 [=10= 之前先删除 PrefixModules 的要求],因为外键声明为 module = models.ForeignKey(Module, on_delete=models.PROTECT)

这个问题教会了我正确命名模型的重要性

出现问题是因为我为模型使用了与 ForeignKey 和根模型相同的名称。

因此,“需要删除以下受保护的相关对象”消息似乎指的是它自己,而实际上它的意思是:

Deleting the ROOT MODEL module 'FR menu' would require deleting
the following protected related objects:

Module FOREIGNKEY USED BY PREFIX: FR menu

长期解决方案是避免模棱两可的模型名称多次使用同名