0

I am deleting a related model from a form (nearly the same way as django admin), I have an item which has many images, image belongs to Item (ForeignKey). I have implemented a post_delete signal to delete thumbnails and the image when deleting the image/images from the form, the problem is that in the post_delete thumbnails are deleted but the image entry in the db remains:

# Auto-delete files from filesystem when they are unneeded:
@receiver(models.signals.post_delete, sender=ImageModel)
def auto_delete_file_on_delete(sender, instance, **kwargs):
    """Deletes file from filesystem
    when corresponding `ImageModel` object is deleted.
    """
    if instance.imagefile:
        thumbmanager = get_thumbnailer(instance.imagefile)
        thumbmanager.delete()

If I add the following in the end, the image instance is also deleted (expected behavior):

# Auto-delete files from filesystem when they are unneeded:
@receiver(models.signals.post_delete, sender=ImageModel)
def auto_delete_file_on_delete(sender, instance, **kwargs):
    """Deletes file from filesystem
    when corresponding `ImageModel` object is deleted.
    """
    if instance.imagefile:
        thumbmanager = get_thumbnailer(instance.imagefile)
        thumbmanager.delete()
        instance.delete()

Am I missing something? Shouldn't the post_delete signal be sent after deleting the instance? Why is the instance persisted in the db? I assume that this has to do with Queryset delete behavior, but I am sceptical to it since If I remove the signal then the instance of ImageModel is deleted (though thumbnails do remain in the database). Mind that the model uses the django build in ImageField field, and not easy thumbnails provided fields:

class ImageModel(models.Model):
    ...
    imagefile = models.ImageField(upload_to="properties/%m/%Y")
    item = models.ForeignKey('app.ItemModel', related_name='images')
    ...

The above solution (instance.delete()) is working with no problems, just curious about the behavior.

4

2 回答 2

1

您也可以使用django-cleanup,当您删除模型时,它会自动调用 FileField 上的 delete 方法。

pip install django-cleanup

设置.py

INSTALLED_APPS = (
     ...
    'django_cleanup', # should go after your apps
)
于 2015-03-11T12:18:09.043 回答
0

好的,它可以添加删除(save=False):

# These two auto-delete files from filesystem when they are unneeded:
@receiver(models.signals.post_delete, sender=ImageModel)
def auto_delete_file_on_delete(sender, instance, **kwargs):
    """Deletes file from filesystem
    when corresponding `ImageModel` object is deleted.
    """
    if instance.imagefile:
        thumbmanager = get_thumbnailer(instance.imagefile)
        thumbmanager.delete(save=False)
于 2014-01-17T11:44:29.990 回答