Hi All,

I have a Model with an image field and I want to be able to change the 
image using a ModelForm. When changing the image, the old image should be 
deleted and replaced by the new image. 

I have tried to do this in the clean method of the ModelForm like this:

    def clean(self):
    cleaned_data = super(ModelForm, self).clean()

    old_profile_image = self.instance.image
    if old_profile_image:
        old_profile_image.delete(save=False)        
    return cleaned_data

This works fine unless the file indicated by the user is not correct (for 
example if its not an image), which result in the image being deleted 
without any new images being saved. I would like to know where is the best 
place to delete the old image? By this I mean where can I be sure that the 
new image is correct before deleting the old one? I prefer to do this in my 
ModelForm class if possible but doing it in the Model class would also be 
acceptable. 

-- 
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/-/kB0VSrr9O1MJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to