Hi,

You shouldn't do it in the view, do it in the database model instead. It
won't do anything to any other fields than the image field the way I wrote
the function (all other fields are untouched).

The reason you shouldn't do it in the view, is because if you were to
update the image any other place than via the view, the code wouldn't run.
It's more sensible and more DRY to do it in the model.

Regards,

Andréas

2015-03-24 14:10 GMT+01:00 Akash Patni <akash.pa...@ranosys.com>:

> Hey,
> I am new to django. I have other feilds also other than image field in my
> form, so will you please tell me how to use above mentioned function in
> edit_profile function in view, Can i call another function from another
> function in views.py, if yes please tell me
>
> On Tue, Mar 24, 2015 at 6:11 PM, Andreas Kuhne <andreas.ku...@suitopia.com
> > wrote:
>
>> 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):
>>   if instance.pk:
>>     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.pa...@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+unsubscr...@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
>>> <https://groups.google.com/d/msgid/django-users/ca9fa002-cbd5-4bbf-b42c-2a6ce88e8e0c%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/oZrAdWgzwo0/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscr...@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
>> <https://groups.google.com/d/msgid/django-users/CALXYUbkpM6op303LMxA2iGNuzqgzB%3DjBPWw2KXkpjwdaho2Z%2BQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> *Best Regards,*
> Akash Patni
> Software Engineer
>
>
>
>
> *[image: ranosys] <http://ranosys.com/>  [image: facebook]
> <https://www.facebook.com/ranosys>[image: twitter]
> <https://twitter.com/ranosys>[image: linkedin]
> <https://www.linkedin.com/company/741079?trk=prof-exp-company-name>[image:
> googleplus] <https://plus.google.com/+Ranosys> Head Office: Oxley Bizhub,
> #06-48 | 73 Ubi Road 1 | Singapore - 408733Tel: +65 66331556  |  HP: +65
> 98573420Global Offices:San Francisco, USA | Jaipur, India | Bikaner, India*
>
> --
> 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+unsubscr...@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/CANSMZiTGE4rue_LtY3YrsERGqBuy%3DoFX0eJb_9o81R_us-OibQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CANSMZiTGE4rue_LtY3YrsERGqBuy%3DoFX0eJb_9o81R_us-OibQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> 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+unsubscr...@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/CALXYUb%3DxUnSdyTevvw3oSwA%2B1kpvMwBXednRxkswXMUkMdU78g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to