Wednesday, February 25, 2015

Re: Delete the file related on the FileField

Hi Karim,

You can do:

from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver

@receiver(pre_delete, sender=ModelName)
def modelname_delete(sender, instance, **kwargs):
    instance.field_name.delete(False)

or on your model you can write your delete method:

def delete(self, *args, **kwargs):
        self.field_name.storage.delete(self.field_name.path)
        return super(ModelName, self).delete(*args, **kwargs)


Best regards,
Rodrigo Zayit

On Tuesday, 24 February 2015 22:30:53 UTC-3, Karim Gorjux wrote:
I'm using a form that get an image and store it in the database. Doing my (manual) tests I discover that the deleting the FileField doesn't remove the associated file on the hard disk. Is there a design pattern to handle the remaining file?

Thank you.

--
Karim N. Gorjux

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd066bec-20d6-4162-b9bf-a74803f3f407%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment