djangopostgresqldjango-modelsdjango-adminmutual-recursion

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


In Django Admin, I have a model called Modules.

When I try to delete a Module object, I get the following error:

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

Module: FR menu

I understand why this would happen if the protected object had been a different model, but in this case, the warning refers to itself.

I don't even know where to begin to debug... perhaps it's because of an old migration? . This model is a ForeignKey model for a model called Prefix this one — here is the Module model declaration:


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"]

And here is the Prefix model declaration that includes Modules as a ForeignKey:

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"]

Solution

  • Module seems to refer to PrefixModules based on its verbose_name, so that error most probably refers to the requirement to delete PrefixModules first before Module, as the foreign key is declared as module = models.ForeignKey(Module, on_delete=models.PROTECT)