0

My models:

class Spectacle(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100 unique=False)
    description = models.TextField()

class SpectacleGallery(models.Model):
    spectacle = models.ForeignKey(Spectacle)
    image = models.ImageField(upload_to=upload_path_handler, max_length=255, help_text=_(u'tylko pliki z rozszerzeniem .jpg'))
    image_thumb = models.CharField(max_length=255, editable=False, blank=True, null=True)

    def delete(self, *args, **kwargs):
        image_thumb_abs = settings.MEDIA_ROOT + str(self.image_thumb)
        # try to delete thumb and image
        try:
            remove(image_thumb_abs)
        except Exception:
            pass

        try:
            self.image.delete()
        except Exception:
            pass

        super(SpectacleGallery, self).delete(*args, **kwargs) # Call the "real" delete() method.

        #try to delete dir
        try:
            removedirs(settings.SPECTACLE_GALLERY_UPLOAD_PATH + str(self.spectacle_id))
        except:
            pass

Now in admin, when I delete from SpectacleGallery file is being deleted from disk and DB properly. The problem is when I want to delete Spectacle. Deletion page is asking if i'm sure to delete Spectacle and all related images. I confirm and: all records from database are deleted. Unfortunately files from SpectacleGallery are not deleted.I paste print into delete() method in SpectacleGallery. Nothing shows so my question is: Why delete method is not called when deleting it in that way?

robos85
  • 2,484
  • 5
  • 32
  • 36

1 Answers1

0

my guess is that it is similar to when you delete items in bulk. Django does a different kind of query to execute the delete in a more efficient way rather than looking through each related item and calling delete on the model.

(explained here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/)

Aaron
  • 4,206
  • 3
  • 24
  • 28