R
R
Ruben Harutyunov2014-04-17 22:28:42
Django
Ruben Harutyunov, 2014-04-17 22:28:42

How to implement image resizing on upload from Django admin?

Hello, there is an ImageField field and I need to make it possible to set the image size on upload in the admin panel, that is, to display fields in the admin panel where the user can set the image size (width and height) himself. Is there a way to make this possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ali Aliyev, 2014-04-17
@K_DOT

Override save method in model class?

class Model(model.Model):
    _image=models.ImageField(upload_to='folder')
    thumb=models.ImageField(upload_to='folder')
    description=models.CharField()

    def set_image(self, val):
        self._image = val
        self._image_changed = True

        # Or put whole logic in here
        small = rescale_image(self.image,width=100,height=100)
        self.image_small=SimpleUploadedFile(name,small_pic)

    def get_image(self):
        return self._image

    image = property(get_image, set_image)

    # this is not needed if small_image is created at set_image
    def save(self, *args, **kwargs):
        if getattr(self, '_image_changed', True):
            small=rescale_image(self.image,width=100,height=100)
            self.image_small=SimpleUploadedFile(name,small_pic)
        super(Model, self).save(*args, **kwargs)

Taken from here stackoverflow.com/questions/4269605/django-overrid...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question