Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. <strong><em>=> I still don't know why..., because I expected that the cached image is always deleted when the original is deleted.</em></strong></p> <p>Using the following code everything works as expected: Basically I made sure the new uploaded image has always another filename:</p> <p>Model.py</p> <pre><code>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}) </code></pre> <p>View.py:</p> <pre><code># 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 </code></pre> <p>Hopefully this can help out others as well.</p> <p>Kind Regards</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload