Since some image files have name conflicts, I decided to make the system to change a uploaded file's name automatically. However, after changing the system, I got in trouble with getting a current date for the file path.
This is how my previous Image model looks like, and it stores an image with a name like boutique/index/2018/9/20/FILE_NAME.jpg.
class Image(TimeStampedModel):
...
image = ImageField(..., upload_to='boutique/index/index/%Y/%m/%d')
...
After that, I changed it to like this. This successfully changes a uploaded image's name automatically. However, it stores a name like boutique/%Y/%m/%d/FILE_NAME.jpg.
def image_path(instance, filename):
basefilename, file_extension = os.path.splitext(filename)
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
randomstr = ''.join((random.choice(chars)) for x in range(10))
return 'boutique/index/%Y/%m/%d/{imageid}/{basename}{randomstring}{ext}'.format(imageid=instance.store.domainKey, basename=basefilename, randomstring=randomstr, ext=file_extension)
class Image(TimeStampedModel):
...
image = ImageField(..., upload_to=image_path)
...
Like you see the above, the problem is that %Y, %m, and %d don't provide date data I need anymore. What is wrong here? image_path function returns the same thing in the same place. I don't know why they are just like recognized as a normal string