Is it possible to update ImageSpecField with a source's new file path?

21 views
Skip to first unread message

Phil Gyford

unread,
Aug 27, 2021, 6:57:59 AM8/27/21
to Django ImageKit
Hi,

I have a Django model with a picture (ImageField) and then a thumbnail (ImageSpecField) that uses picture as its source.

I'm overriding the model's save() method and, after its initial save, moving the original image file to a new location, updating picture.name to reflect this, and saving the model again.

This seems to work OK unless I'm using the Optimistic Cachefile Strategy which (I think) causes the ImageSpecField to generate the thumbnail file on save – it only knows the original file's original path, so this fails with a "The file cannot be reopened" error.

Is there some way I can tell ImageSpecField where its source field's image file is now located? I've tried to work it out from reading its code but can't figure it out.

Thanks,
Phil

Phil Gyford

unread,
Sep 2, 2021, 12:39:58 PM9/2/21
to Django ImageKit
I found what I was missing, so in case anyone else comes to this in the future... 

I didn't have to do anything to the ImageSpecField itself. Although I'd updated picture.name I hadn't updated picture's File object. So I had to set picture.file.path to the new path where the image file had been moved to.

After that, imagekit generated the CACHE files in the expected location.

Phil
Reply all
Reply to author
Forward
0 new messages