Сыч
Сыч

Reputation: 929

Avoid copying when adding a large file to FileField

I'm dealing with some quite large files which are uncomfortable to upload via http, so my users upload files using FTP which my code then needs to move into FileField.upload_to (where they normally end up when uploaded via HTTP). My problem is, the commonly suggested method of using django.core.files.File:

from django.core.files import File

# filename is a FileField
file_obj = MyModel(filename=File(open('VIDEO_TS.tar', 'rb')))

leads to copying the data, which i need to avoid. Is there any way to add the already existing file to a FileField while making sure upload_to is called?

Upvotes: 1

Views: 422

Answers (2)

Mikhail Korobov
Mikhail Korobov

Reputation: 22238

a bit late, but:

class _ExistingFile(UploadedFile):
    """ Utility class for importing existing files to FileField's. """

    def __init__(self, path, *args, **kwargs):
        self.path = path
        super(_ExistingFile, self).__init__(*args, **kwargs)

    def temporary_file_path(self):
        return self.path

    def close(self):
        pass

    def __len__(self):
        return 0

usage:

my_model.file_field.save(upload_to, _ExistingFile('VIDEO_TS.tar'))

Upvotes: 3

Almad
Almad

Reputation: 5903

I'd say that easiest way would be writing your own field or storage.

Upvotes: 0

Related Questions