matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.24k stars 276 forks source link

Generate images in postsave #548

Open stiefenswiegel opened 2 years ago

stiefenswiegel commented 2 years ago

Hi,

i would like to generate all temp image sin a postsave command. How can i generate the images for a specific instance?

armanexplorer commented 2 years ago

In case you are using ImageSpecField in your model, this could be a workaround:

from imagekit.cachefiles import ImageCacheFile

def generate_temp_images(obj):
  for attr in dir(obj):
    try:
        val = getattr(obj, attr)
        if isinstance(val, ImageCacheFile):
            val.generate(force=True)
    except:
        pass

As a note, the value of fields with ImageSpecField type are determined by ImageSpecFileDescriptor descriptor returning ImageCacheFile objects. So, we can use the value of these fields to generate our temp files.