jason
jason

Reputation: 3075

ImageFieldFile object has no attribute startswith

Using the django-storages package S3 on the delete signal I'm trying to delete the image from S3 I have tried the following (see below) but get the error...

'ImageFieldFile' object has no attribute 'startswith'

def product_pre_delete(sender, instance, **kwargs):
    """
    Sent at the beginning of a product delete() method product queryset's delete() method.
    """
    default_storage.delete(instance.qr_image)

models.signals.pre_delete.connect(product_pre_delete, sender=Product)

Upvotes: 0

Views: 2302

Answers (2)

Arctelix
Arctelix

Reputation: 4576

I ran across this issue as well and found that the 'ImageFieldFile' object has no attribute 'startswith' error is thrown because you are using the file object rather then file.name. Using the object works locally, but not with AWS. The below code should work as I was able to successfully delete a file that is still associated with an object.

default_storage.delete(instance.qr_image.name)

Upvotes: 1

Brandon Taylor
Brandon Taylor

Reputation: 34553

As far as I know, django-storages will only delete the file when the object itself is deleted (scroll down to the section on delete) https://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html?highlight=delete

If you want to keep the object, but delete the file, you'll probably have to handle that yourself using Boto, which is the underlying library used by django-storages when working with S3.

Upvotes: 1

Related Questions