Django upload_to outside of MEDIA_ROOT
While the accepted answer is probably what you want, we now have the option with django
3.1 that we can decide which storage to use at runtime by passing a function to the storage argument of an ImageField
or FileField
.
def select_storage():
return MyLocalStorage() if settings.DEBUG else MyRemoteStorage()
class MyModel(models.Model):
my_file = models.FileField(storage=select_storage)
Have a look at the official docs.
Please note that the current accepted answer writes the actual value of the variable UPLOAD_ROOT
to the migration file. This doesn't produce any SQL
when you apply it to the database but may be confusing if you change that setting frequently.
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