Last active
May 20, 2022 19:56
django model filefield random upload path
This file contains 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
# helpers.py | |
@deconstructible | |
class RandomFileName(object): | |
def __init__(self, path): | |
self.path = os.path.join(path, "%s%s") | |
def __call__(self, _, filename): | |
# @note It's up to the validators to check if it's the correct file type in name or if one even exist. | |
extension = os.path.splitext(filename)[1] | |
return self.path % (uuid.uuid4(), extension) | |
# models.py | |
class Foo(models.Model): | |
image = models.ImageField(upload_to=RandomFileName('my_app/model')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@deconstructible
class RandomFileName(object):
uuid1() more safe to protect from duplicate