Rich
Rich

Reputation: 1779

Upload to absolute path in Django

I am trying to get upload_to of FileField to be an absolute path rather than relative to MEDIA_ROOT. If I make the path absolute I get a 400 error on post of file. If no leading / it stores under MEDIA_ROOT. The uploaded document needs to be held securely and not under MEDIA_ROOT but I also have images that need to go into MEDIA_ROOT so can't change it to be out public area.

This is my code...

class Document(models.Model):
def get_upload_path(instance, filename):
    path = os.path.join( settings.DOCUMENT_DIR,  str(instance.client.id), 'documents',  str(instance.id), filename)
    return path

uploaded = models.FileField(null=True, blank=True, upload_to=get_upload_path, max_length=255)

Any ideas?

Upvotes: 0

Views: 2472

Answers (1)

dgel
dgel

Reputation: 16796

You should be able to create a different FileSystemStorage instance for each storage location.

Alternatively, you could write a custom storage system to handle the files.

Upvotes: 1

Related Questions