Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 在将大文件添加到FileField时避免复制_Django_Filefield - Fatal编程技术网

Django 在将大文件添加到FileField时避免复制

Django 在将大文件添加到FileField时避免复制,django,filefield,Django,Filefield,我正在处理一些非常大的文件,这些文件很难通过http上传,因此我的用户使用FTP上传文件,然后我的代码需要移动到FileField.upload_to(通常通过http上传时,他们会在这里结束)。我的问题是,通常建议使用django.core.files.File: from django.core.files import File # filename is a FileField file_obj = MyModel(filename=File(open('VIDEO_TS.tar',

我正在处理一些非常大的文件,这些文件很难通过http上传,因此我的用户使用FTP上传文件,然后我的代码需要移动到FileField.upload_to(通常通过http上传时,他们会在这里结束)。我的问题是,通常建议使用
django.core.files.File

from django.core.files import File

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

导致复制数据,这是我需要避免的。在确保调用upload\u to的同时,有没有办法将已经存在的文件添加到文件字段?

我认为最简单的方法是编写自己的字段或存储

有点晚了,但是:

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
用法:

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