0

I am using a custom field to upload image on s3 using django 1.9. I want to delete image from s3 whenever model instance is deleted. I have tried post_delete signal with ImageField's delete() method but since I'm using custom field cannot achieve the result. Any suggestion on how to achieve this?

amie
  • 1

1 Answers1

0
from django.db.models.signals import pre_delete

.....

pre_delete.connect(delete_image, dispatch_uid="delete_image")

.....

def delete_image(sender, instance, **kwargs):
    for field_name in instance._meta.get_fields():
        try:
            field = getattr(instance, field_name)
        except:
            field = None
        if isinstance(field, your_custom_field):
            your_app_utils.clean_images(field.get_images())
selimoves
  • 61
  • 3
  • I'm new to django. can you please explain how can I clean images from the custom field. – amie Feb 21 '18 at 22:43