S
S
Sergey Nizhny Novgorod2016-08-19 13:32:06
Django
Sergey Nizhny Novgorod, 2016-08-19 13:32:06

How to set a unique upload_to path when saving an image?

Hello.
The situation is as follows:
- the user enters his picture
- a view is made from the picture (easy thumbnails - gives them all the same name).
- The picture is saved in the ImageFiled field of the user's profile
As a result, a situation is obtained when each subsequent user uploads his avatar and overwrites the previous picture.
I did not find how to set a unique name for easy-thumbnails, so I want to know how to make a unique path for "upload_to" I
found in the documentation - year, month, day, but I would like something like user id - is this possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stayHARD, 2016-08-19
@Terras

import os
import re


def get_file_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    return os.path.join(instance.__class__.__name__.lower(), filename)

And then upload_towriteget_file_path

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question