I have three model classes in a Django app:
class Folder(models.Model):
...
folder = models.ForeignKey('Folder',null=True,blank=True,related_name='folders')
front_thumbnail_image = models.ForeignKey('Image',verbose_name='Front Thumbnail',null=True,blank=True,related_name='front_thumbnail_for_folders')
middle_thumbnail_image = models.ForeignKey('Image',verbose_name='Middle Thumbnail',null=True,blank=True,related_name='middle_thumbnail_for_folders')
back_thumbnail_image = models.ForeignKey('Image',verbose_name='Back Thumbnail',null=True,blank=True,related_name='back_thumbnail_for_folders')
class Image(models.Model):
...
folder = models.ForeignKey(Folder,related_name='images',null=True)
class ImageRepresentation(models.Model):
...
image = models.ForeignKey(Image, related_name="image_representations")
Given this model, when I delete an Image in the admin site, I would expect the ImageRepresentations associated with that Image to be deleted as well, and the Folder enclosing that Image to be left alone.
The admin site tells me that the enclosing Folder will be deleted as well. What can I do to get the desired behavior? I looked into delete cascade rules, but nothing I tried seemed to work.
Edited to add three foreign keys on Folder (the thumbnail image ones)… I totally overlooked those (obviously). There are no other relationships, honest.
When you create the
ForeignKey, what gets deleted when the parent object is deleted is controlled by theon_deleteparameter.By default,
on_deleteis set tomodels.CASCADE, which will delete children objects when the parent is deleted. None of the other configuration options can create the behavior you are reporting here.Do you have a
ForeignkeyfromFoldertoImageorImageRepresentation? (folder thumbnail in your case)