当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Django FileField how to get real temp uploaded file path?

问题描述:

I'm creating a new class for file field to upload selected file on server through FTP. When in method pre_save(), I'm trying to get file path this is raising below error:

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

Here is my class:

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_

And also I don't want to save uploaded file, just wanting to use temporary uploaded local file and upload on FTP and the remove from local.

分享给朋友:
您可能感兴趣的文章:
随机阅读: