How to save django FileField to user folder?

I have a model like this


def upload_location(instance, filename):
    return 'validate/%s/builds/%s' % (get_current_user(), filename) 

class MidletPair(models.Model):
    jad_file = models.FileField(upload_to = upload_location)
    jar_file = models.FileField(upload_to = upload_location)
    upload_to=tempfile.gettempdir()

How can I get the current user in upload_location () ...?

Side note: django's item search is confusing since there are a lot of things on the net up to 1.0.

+3
source share
3 answers

The current user is stored in the request object, and you cannot receive it in the model method unless you transfer it from another location, which you cannot do in the upload_to function.

, - - . __init__ , . .

+8

, upload_location .

+5

Source: https://habr.com/ru/post/1708263/


All Articles