How to store thumbnails in the same folder with Django ImageKit?

401 Views Asked by At

Does anyone have experience using ImageKit to manage thumbnails?

I currently have the following in my models.py:

class Item(models.Model):
    id = models.AutoField(primary_key=True)
    owner = models.ForeignKey(
        get_user_model(),
        on_delete=models.SET_NULL,
        null=True,
        blank=True
    )
    image = ProcessedImageField(
        upload_to=image_upload,
        blank=True,
        validators=[validate_image],
        format='JPEG',
        help_text="Max file size is 3 MB."
    )
    image_thumbnail = ImageSpecField(
        source='image',
        processors=[ResizeToFill(50, 50)],
        format='JPEG',
        options={'quality': 60}
    )

I'd like to rename the thumbnail and store it in a particular folder (not the CACHE/images/ folder that ImageKit defaults to), but can't figure out how to do that (and adding an "upload_to" to the thumbnail gives me an error). All help greatly appreciated! Thank you!

2

There are 2 best solutions below

2
crimsonpython24 On BEST ANSWER

According to the docs:

ImageSpecFields, on the other hand, are virtual—they add no fields to your database and don’t require a database. This is handy for a lot of reasons, but it means that the path to the image file needs to be programmatically constructed based on the source image and the spec.

You might just want to stick to the ProcessedImageFIeld if you don't want your files to be stored in a cache folder.

0
Christoffer On

In your settings file, if you set:

IMAGEKIT_CACHEFILE_DIR = ""

then the images will be saved in a subfolder of the same folder the original image is in. This instead of storing it in a /CACHE/images/ folder.