Django upload_to outside of MEDIA_ROOT

2019-01-22 12:24发布

问题:

My deployment script overwrites the media and source directories which means I have to move the uploads directory out of the media directory, and replace it after the upload has been extracted.

How can I instruct django to upload to /uploads/ instead of /media/?

So far I keep getting django Suspicious Operation errors! :(

I suppose another solution might be a symlink?

Many thanks, Toby.

回答1:

I did the following:

from django.core.files.storage import FileSystemStorage

upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/uploads')

image = models.ImageField(upload_to='/images', storage=upload_storage) 

UPLOAD_ROOT is defined in my settings.py file: /foo/bar/webfolder/uploads