Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:14:18+00:00 2026-06-07T16:14:18+00:00

I have a Model with an image field and I want to be able

  • 0

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?

Edit:
I prefer to do this in my ModelForm class if possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:14:19+00:00Added an answer on June 7, 2026 at 4:14 pm

    Ok I figured out how to do it in a clean way using a ModelForm. Basically I create a class called ‘ChangeImageForm’, which will make sure to delete the old image when changing an image. Furthermore it also works if an image is invalid:

    class ChangeImageForm(ModelForm):
    
        def clean(self):
            cleaned_data = super(ModelForm, self).clean()
            self.old_image = self.instance.image
            return cleaned_data
    
        def save(self, commit=True):
            instance = super(ChangeImageForm, self).save(commit)
    
            if self.old_image:
                self.old_image.delete(save=False)
    
            return instance
    

    Now I can easily add the above functionality to a ModelForm with an image field by inheriting from ChangeImageForm like this:

    class MyModelForm(ChangeImageForm):
        class Meta:
            model = MyModel # model with an image field
    

    I think overriding the ModelForm is the best place to do this, instead of in the Model class, since I only want to delete the old image in some cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC and have a model which has an image (byte array)
I want users to be able to upload an image and have it be
I have a model ModelA with a field of type ImageField. Now, I want
I have an image model and want to assign differnt, pre-choosen tags to the
I have given my code below. It is having a image field. I'm using
I have a model containing ImageField which should be resized after uploading. class SomeModel(models.Model):
I have a model Image and every image is having description. User can add
In my model I have: has_attached_file :image, :storage => :s3, :styles => { :original
I'm using sorl-thumbnail to get some images to crop. I have a model looking
I have model with ImageFile field: def upload_course_cover(object, filename): return '/media/courses/%s_%s' % (Course.objects.aggregate(Max('id'))['id__max'] +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.