Django FileField如何获取真正的临时上传文件路径?

时间:2014-08-12 07:23:13

标签: python django

我正在为文件字段创建一个新类,以通过FTP在服务器上上传所选文件。在方法pre_save()中,我试图获取文件路径,这会引发错误:

The 'auth_img' attribute has no file associated with it.

这是我的班级:

class XFileField(FileField):

    attr_class = FieldFile
    descriptor_class = FileDescriptor
    description = _("File")

    def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
        self._unique_set_explicitly = 'unique' in kwargs
        self._primary_key_set_explicitly = 'primary_key' in kwargs

        if 'ext_whitelist' in kwargs:
            ext_whitelist = kwargs.pop('ext_whitelist')
            self.ext_whitelist = [i.lower() for i in ext_whitelist]

        super(XFileField, self).__init__(verbose_name, name, upload_to, **kwargs)

    def pre_save(self, model_instance, add):
        file_ = super(XFileField, self).pre_save(model_instance, add)

        if file_ is not None:
            file_.save(file_.name, file_, save=True)  # Raising Error Here
            '''
            Here File will upload on server through FTP.
            '''
        return file_

而且我也不想保存上传的文件,只是想使用临时上传的本地文件并在FTP上传和从本地删除。

0 个答案:

没有答案