Reputation: 5793
I'm trying to force certain FilerImageFields
to be convert to another format on saving.
I've created this model
class SearchImage(FilerImageField):
def convert_to_webp(self):
print("in convert")
extension = ['jpeg', 'png', 'jpg', 'img', 'gif']
if any(ext in self.file.name.lower() for ext in extension):
#try:
img = Image.open(self.file)
correggi_nome = self.file.name.split('.')[0]
img.save(correggi_nome + '.webp','webp')
logger.debug('img.save save another copy of the file not repalced the original!')
#except Exception as ex:
# logger.error(ex)
def save(self, *args, **kwargs):
self.convert_to_webp()
print("Save search image")
super().save()
class Organisation(models.Model):
....
search_image = SearchImage(
related_name='+',
verbose_name=_('Search thumbnail image'),
help_text=_('Search thumbnail image'),
on_delete=models.SET_NULL,
blank=True,
null=True
)
....
def save(*args, **kwargs):
print('saving')
print("just before search thumbnail save")
self.search_image.save()
print("just after search thumbnail save")
super.save()
my SeachImage.save()
is never called. When I save an image (or organisation) I see the print statements within the Organisation.save() but not the SearchImage.save(). How to I get this to work?
Upvotes: 0
Views: 73
Reputation: 767
Okay so I went down a bit of a rabbit hole but it looks like you want to overwrite the Image object's save method, not the FilerImageField, which is a ForeignKey object:
class MyImageModel(Image):
def convert_to_webp(self):
print('in convert')
extension = ['jpeg', 'png', 'jpg', 'img', 'gif']
if any(ext in self.file.name.lower() for ext in extension):
print('Doing the conversion')
def save(self, *args, **kwargs):
self.convert_to_webp()
print('Save search image')
super().save(*args, **kwargs)
class SearchImage(FilerImageField):
default_model_class = MyImageModel
class Organisation(models.Model):
search_image = FilerImageField(
verbose_name='Search thumbnail image',
help_text='Search thumbnail image',
on_delete=models.SET_NULL,
blank=True,
null=True
)
def save(self, *args, **kwargs):
self.search_image.save()
super().save(*args, **kwargs)
Upvotes: 1