D
D
Dmitry Ivanov2018-02-08 12:28:07
Django
Dmitry Ivanov, 2018-02-08 12:28:07

How to change filename on upload in Django?

How can I change the file name when uploading in Django to my own, preferably a UUID?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-08
@DmitrIvanov

from os.path import splitext
from uuid import uuid4
from django.core.files.storage import FileSystemStorage

class UUIDFileStorage(FileSystemStorage):
    def get_available_name(self, name, max_length=None):
        _, ext = path.splitext(name)
        return uuid4().hex + ext

class SomeModel(models.Model):
    some_filed = models.ImageField('Какое-то изображение', upload_to='someimages', storage=UUIDFileStorage())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question