Tuesday, March 24, 2015

Re: image handling

Hi,

Ok, so what you should do is add a signal to pre save and then delete the old image if the new image is attached.

Something like this should work:

from django.core.files.storage import default_storage as storage
from django.db.models.signals import pre_save


def image_delete(sender, instance, *args, **kwargs):
    old_instance = User.objects.get(pk=instance.pk)
    
    if old_instance.image != instance.image and instance.image:
      storage.delete(old_instance.image.name)

pre_save.connect(image_delete, sender=User)

You can see more information about signals here: https://docs.djangoproject.com/en/1.7/topics/signals/

Regards,

Andréas

2015-03-24 11:40 GMT+01:00 <akash.patni@ranosys.com>:
When i edit image that is when i upload new image,the old image should get deleted

On Tuesday, March 24, 2015 at 2:40:35 PM UTC+5:30, akash...@ranosys.com wrote:
hi..
Can anyone please tell me how to handle old image after uploading new image.

--
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/ca9fa002-cbd5-4bbf-b42c-2a6ce88e8e0c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/CALXYUbkpM6op303LMxA2iGNuzqgzB%3DjBPWw2KXkpjwdaho2Z%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment