Skip to content

Instantly share code, notes, and snippets.

@bakatrouble
Last active July 16, 2017 11:55
Show Gist options
  • Save bakatrouble/fe6ff0dea3e3ad4ae7c3c613b94a2aeb to your computer and use it in GitHub Desktop.
Save bakatrouble/fe6ff0dea3e3ad4ae7c3c613b94a2aeb to your computer and use it in GitHub Desktop.
Runtime generation of uploaded file name (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)
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