import os import uuid from django.utils.deconstruct import deconstructible # Ref: https://stackoverflow.com/questions/2673647/enforce-unique-upload-file-names-using-django @deconstructible class RandomFileName(object): def __init__(self, path): self.path = os.path.join(path, "%s%s") def __call__(self, _, filename): extension = os.path.splitext(filename)[1] return self.path % (uuid.uuid4(), extension)