Thursday, November 15, 2012

Re: managing files, deleting file from file system with admin site

I tried to override delete() method of Car model and it works when i delete single object using admin site.
But it doesnt work with deleting many objects at once.
So i ended up with this:

def post_delete_image_deleting(sender, instance, **kwargs):
    storage, path = instance.photo.storage, instance.photo.path
    storage.delete(path)

def pre_save_image_deleting(sender, instance, **kwargs):
    try:
        old_instance = Car.objects.get(pk = instance.pk)
    except:
        pass
    else:
        if instance.photo != old_instance.photo:
            storage = old_instance.photo.storage
            path = old_instance.photo.path
            storage.delete(path)

post_delete.connect(post_delete_image_deleting, sender=Car)
pre_save.connect(pre_save_image_deleting, sender=Car)

It works for me.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/74uDgd_67VMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate