I'm using imagekit provided at: imagekit
So, I've defined two class model:
class Photo(models.Model):
#photo_wrapper = models.ForeignKey(PhotoWrapper, blank=True, null=True)
original_image = models.ImageField(upload_to='static/photos')
thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1),
resize.Crop(50, 50)], image_field='original_image',
format='JPEG', quality=90)
num_views = models.PositiveIntegerField(editable=False, default=0)
class IKOptions:
# This inner class is where we define the ImageKit options for the model
spec_module = 'myspecs.specs'
cache_dir = 'static/photos'
image_field = 'original_image'
save_count_as = 'num_views'
class Country(models.Model):
country_name = models.CharField(max_length=250)
country_photo = models.ForeignKey(Photo, blank=True, null=True)
def __unicode__(self):
return '%s' % self.country_name
The problem is that each photo is created in the "static/photos" path. My intent is to save the image and the thumbnail with a dynamic path, based on the country name..
For example, for the country "Argentina", the dynamic path will be "static/photos/Argentina/"
How can I accomplish that?
It looks like you're mixing two different versions of ImageKit. The newer versions (1.0+) no longer use the inner
IKOptions
class, so all of that is being ignored. (The save_count_as functionality was also removed.)If you want to control the cache filename, the
ImageSpec
constructor accepts acache_to
kwarg which—likeImageField
'supload_to
—can be a callable. Here's the current documentation forcache_to
:So you'll just have to create a function that accepts those arguments and returns the path you want, and use that in your model like so: