tags:

views:

88

answers:

2

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?

A: 

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

Almad
+1  A: 

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'))
Mike Korobov
thanks, i'll try this when i get to my code
RommeDeSerieux