[Answered ]-Django ImageKit: How to rename uploaded images?

2👍

you can change upload_to value to function like this

def upload_to_id_image(instance, filename):
    extension = splitext(filename)[1].lower()
    salt, hashed = generate_sha1(instance.id)
    path = 'profiles/%(id)s_%(date_now)s_' % {
                                         'id': instance.user.id,
                                         'date_now': get_datetime_now().date().strftime("%Y%m%d")}
    return '%(path)s%(hash)s%(extension)s' % {'path': path,
                                          'hash': hashed[:16],
                                          'extension': extension}

and then, you should change your code like this,

photo = ProcessedImageField(upload_to=upload_to_id_image,

of course you can delete hash code.
but for file security, make hashed file name is better.

Leave a comment