After the problems that I had on this thread , there is still a big problem in my models.py when I use Django Admin. Here is my code (I removed material not related to my problem):
from django.core.files.uploadedfile import InMemoryUploadedFile from PIL import Image as Img import StringIO class Mymodel(models.Model): photo = models.ImageField(upload_to="photo/", blank=True, null=True) def save(self, *args, **kwargs): width = 500 height = 500 size = (width,height) if self.photo: image = Img.open(StringIO.StringIO(self.photo.read())) (imw, imh) = image.size if (imw>width) or (imh>height) : image.thumbnail(size, Img.ANTIALIAS)
It works, files are downloaded, modified and converted to JPEG if necessary. The problem is, every time I edit it, even when it DOES NOT load a new image, it creates a new image (for example, I first save my model with the image "hello.jpg", then I edit it, this will create a new image called "hello_1 .jpg ", even if I didn’t download anything). I thought the try / except block would only work when editing (so there is no new file download), but apparently not.
Thanks in advance for your help:)
source share