Last active
July 16, 2017 11:55
-
-
Save bakatrouble/fe6ff0dea3e3ad4ae7c3c613b94a2aeb to your computer and use it in GitHub Desktop.
Runtime generation of uploaded file name (Django)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
image = models.ImageField(verbose_name=_('Image'), upload_to=RandomFileName('backgrounds'), | |
help_text=_('Maximum size: 5 MB')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment