0

When I upload a picture of the administration interface I create a scaled-down (thumbnail) version of the picture also. When I delete an image from the administration interface, I want to the small version of the image will also be deleted. How would achieve this?

models.py

from django.db import models
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver
from PIL import Image
import sys
class Picture(models.Model):
    filepath = 'thumbnails/'
    image = models.ImageField(verbose_name="Bild", upload_to='photos', max_length=255, blank=False)
    image_number = models.PositiveSmallIntegerField(verbose_name="Reihenfolge", null=True, blank=False)
    def save(self):
        sizes = {'thumbnail': {'height': 50, 'width': 50}}

        super(Picture, self).save()
        photopath = str(self.image.path)  # this returns the full system path to the original file
        im = Image.open(photopath)  # open the image using PIL

    # pull a few variables out of that full path
        extension = photopath.rsplit('.', 1)[1]  # the file extension
        filename = photopath.rsplit('\\', 1)[1].rsplit('.', 1)[0]  # the file name only (minus path or extension)
        fullpath = photopath.rsplit('\\', 1)[0]  # the path only (minus the filename.extension)

        # use the file extension to determine if the image is valid before proceeding
        if extension not in ['jpg', 'jpeg', 'gif', 'png']: sys.exit()

        # create thumbnail
        im.thumbnail((sizes['thumbnail']['width'], sizes['thumbnail']['height']), Image.ANTIALIAS)
        thumbname = filename + "_thumbnail.jpg"
        im.save(fullpath + '/' + thumbname)
        self.photo_thumb = self.filepath + thumbname

        super(Picture, self).save()

    def image_thumb(self, fixed_height=80):
        def html(url, height, width):
            return u'<img src="%s" height="%spx" width="%spx"/>' % (url, height, width)

        try:
            aspect_ratio = float(self.image.width) / self.image.height
            calculated_width = aspect_ratio * fixed_height
            return html(self.image.url, fixed_height, calculated_width)
        except IOError:
            return html('asd', fixed_height, fixed_height)

    image_thumb.short_description = 'Vorschau'
    image_thumb.allow_tags = True
    class Meta:
        verbose_name = "Bild"
        verbose_name_plural = "Bilder"

@receiver(pre_delete, sender=Picture)
def image_delete(sender, instance, **kwargs):
    instance.image.delete(False)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Viktor
  • 93
  • 1
  • 6
  • Have you looked at sorl-thumbnail? http://sorl-thumbnail.readthedocs.org/en/latest/ – Hodson Dec 09 '13 at 14:50
  • Yes, but in my case it is not good. – Viktor Dec 09 '13 at 15:04
  • 1
    It is unclear where your confusion exists. It seems that you already have a signal handler that runs on a pre_delete...why not delete the thumbnail there? This [question/answer](http://stackoverflow.com/questions/1534986/how-do-i-override-delete-on-a-model-and-have-it-still-work-with-related-delete) may be useful. – Joseph Paetz Dec 11 '13 at 19:36
  • Thank you for your response. In the meantime I found the solution. – Viktor Dec 11 '13 at 19:46
  • You could just use `django-stdimage` but feel free to dive into the code of https://github.com/codingjoe/django-stdimage/blob/master/stdimage/fields.py line 234 and folloing – codingjoe Feb 04 '14 at 14:40

0 Answers0