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 8117027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:01:06+00:00 2026-06-06T04:01:06+00:00

In my project I have a model where I use Imagekit to process an

  • 0

In my project I have a model where I use Imagekit to process an image. When I save an image I have following requirements:

  • rename image and thumbnail to a unique name
  • when a new image is loaded, the old one should be removed (and the thumbnail in the cache should refresh to the new image).

To accomplish this, I use following code:

The model:

def generate_cache_filename(instance, path, specname, extension):
    extension = '.jpg'
    return 'cache/images_upload/%s_%s%s' % (instance.pk, specname, extension)

def generate_image_filename_1(instance, filename):
    filename = '1'
    extension = '.jpg'
    return 'images_upload/%s_%s%s' % (instance.pk, filename, extension)

class Model(models.Model):
    name = models.CharField(max_length=40)
    image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_image_filename_1, format='JPEG', options={'quality': 90})
    thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90})

The form (to delete the image when it is replaced by a new one):

if form.is_valid():
    form_image = form.cleaned_data['image_1']
        try:
            details = Model.objects.get(pk=pk)
            if details.image_1 != form_image:
                details.image_1.delete(save=False)
        except Model.DoesNotExist: 
            pass
        form.save()

The part of renaming the images and replacing image_1 (= loading new and deleting old) works just fine. But for some reason the thumbnail_1 in the cache does not refresh (= is still the thumbnail of the old image).

I think it has something to do with the deletion code in the form, but I can’t figure out why and how to solve it. Someone with suggestions?

UPDATE 1: it has also something to do with the ‘renaming’. I did some extra tests: when I don’t rename the image_1 file, then everything works fine (also refreshing of the thumbnail). But when I load another image with the same name, then I have the same problem: image_1 is updated, but thumbnail_1 is still the thumbnail of the old image.

UPDATE 2: did some more tests and when uploading a new image with the same filename, I definitely enter the if statement in try. So the old image is deleted. According to the documentation of Imagekit, the thumbnail should also be deleted. But this is not the case.

Many thanks!

  • 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-06T04:01:07+00:00Added an answer on June 6, 2026 at 4:01 am

    In the mean time I found a working solution.
    The main reason why the above code didn’t work was because of the thumbnail in the cache that was not deleted after deleting the original image, especially in cases where the new image had the same filename as the previous one => then the thumbnail was never deleted.
    => I still don’t know why…, because I expected that the cached image is always deleted when the original is deleted.

    Using the following code everything works as expected:
    Basically I made sure the new uploaded image has always another filename:

    Model.py

    def generate_cache_filename(instance, path, specname, extension):
        extension = '.jpg'
        return 'cache/images_upload/%s_%s%s' % (instance.pk, specname, extension)
    # generate random string of 10 characters
    def id_generator(size=10, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for x in range(size))
    
    def generate_random_filename(instance, filename):
        filename = id_generator()
        extension = '.jpg'
    return 'images_upload/%s_%s%s' % (instance.pk, filename, extension)
    
    class Model(models.Model):
        name = models.CharField(max_length=20)
        image_1 = ProcessedImageField([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(500, 370)], upload_to=generate_random_filename, format='JPEG', options={'quality': 90})
        thumbnail_1 = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(83, 78)], image_field='image_1', cache_to=generate_cache_filename, format='JPEG', options={'quality': 90})
    

    View.py:

    # thanks to jdi for the help in the for-loop
    if form.is_valid():
            # A for loop is used here, because my database contains 5 images and 5              thumbnails
            image_list = ['image_%d' % i for i in xrange(1,6)]
            for image_name in image_list:
                form_image = form.cleaned_data[image_name]
                try:
                    details = Model.objects.get(pk=pk)
                    if getattr(details, image_name, None) != form_image:
                        getattr(details, image_name, None).delete(save=False)
                except Model.DoesNotExist: 
                    pass
    

    Hopefully this can help out others as well.

    Kind Regards

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

Sidebar

Related Questions

I have a new MVC 3 project, and in it I use model annotations
I have a project which exposes object model to use by different types of
I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login)
I have the following model in a project: class CarAssignment(models.Model): leg = models.ForeignKey(Leg, null=True,
I have this Project model: class Project < ActiveRecord::Base validates :status, :inclusion => {
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I have a Project model and it has some text attributes, one is summary.
Let's say I have a model class called Project, but instead of this: class
I have an existing model for my project. I would like to switch to
I have a data model where a User can like a Project, Suggestion, Comment

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.