[Django]-Upload files to private location in django

4👍

The default FileSystemStorage insists on saving below the MEDIA_ROOT, but you can use a different storage for your private files:

# in your models.py, or in a separate storage.py
upload_storage = FileSystemStorage(location=UPLOAD_ROOT, base_url='/does/not/matter/')

# models.py
class YourModel(models.Model):
    file = models.FileField(upload_to="some/relative/path", storage=upload_storage)

Then use the usual Django facilities for file handling… forms, the admin etc. should all work.

👤sk1p

Leave a comment