1

I want to upload one image(location of which stores in photo field of model mentioned below ) and I want to duplicate that photo to another field thumbnail programmatically.

I tried in the way mentioned in class Picture(models.Model): below. Bytheway ResizedImageField works and i have tested it only for the photo field. Help needed to overwrite the def save(self, *args, **kwargs): method mentioned below.

from django_resized import ResizedImageField

class Picture(models.Model):
    photo = ResizedImageField('photo', upload_to='photos/%Y/%m/%d', size=[636,331])
    thumbnail = ResizedImageField('thumbnail', upload_to='thumbnail/%Y/%m/%d', size=[150, 100], blank=True, null=True)

    def save(self, *args, **kwargs):
        super(Picture, self).save(*args, **kwargs)
        if self.photo:
            self.thumbnail = ResizedImageField(self.photo)
            self.thumbnail.save()
Cœur
  • 37,241
  • 25
  • 195
  • 267
S.Hillol
  • 79
  • 1
  • 8

1 Answers1

1

I think the problem is with the save method you are calling on self.thumbnail.save(), try this instead:

def save(self, *args, **kwargs):
    if self.photo:
        self.thumbnail = ResizedImageField(self.photo)
    super(Picture, self).save(*args, **kwargs)

you may find this post useful

Community
  • 1
  • 1
kt14
  • 838
  • 15
  • 25