62👍
✅
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
2👍
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.
- [Django]-How to redirect with messages to display them in Django Templates?
- [Django]-Django's SuspiciousOperation Invalid HTTP_HOST header
- [Django]-Django comparing model instances for equality
Source:stackexchange.com