pythondjangoimagekit

ImageSpecField not being re-generated on source change


I'm using 3.0.3 of django-imagekit. I've got the following setup on a model:

render_image = models.ImageField(upload_to='spec/', null=True, blank=True)
render_thumbnail = ImageSpecField([ResizeToFill(220, 220)],
                                  source='render_image',
                                  format='JPEG',
                                  options={'quality': 90})

Which is working great to start: when I upload the image initially, the thumbnail is created fine. My problem is that I'm programmatically changing the original image. I'd like to re-generate the thumbnail based on the new one, but I can't see how. I'm changing the original image with this line:

model.render_image = image_rel_path # that is, 'model/uuid.png'

That works fine, and I can access the new image. I'm even able to use the imagekit thumbnail template tag to create a re-sized, up-to-date thumbnail. I'd go with that, but I want just the image url, not a full image tag since it needs to be a background image (imagekit developers: if there isn't a way to just get the url in a template tag, please take note that generating the html violates the principle of least responsibility).

I don't want to dig into imagekit's guts, I'd just like the thumbnail to be properly generated on the 'source file changed' signal by changing how I'm updating the source file, or I'd like to be able to call something like model.render_thumbnail.generate(source=model.render_image).


Solution

  • Here we go. I avoided this at the time, but I came around to needing to fix the problem again, and I discovered a generate method on a ImageSpecField. All you need to do is model.image_thumbnail.generate(). If you get the error MissingSource: The spec '<imagekit.specs.DynamicSpec object at 0x7fb8a93ef450>' has no source file associated with it., the source the ImageSpecField was associated with has no associated file.