我试图从分配给我的磁盘FileField或文件,但我有此错误:
AttributeError的:“海峡”对象有没有属性“打开”
我的Python代码:
pdfImage = FileSaver()
pdfImage.myfile.save('new', open('mytest.pdf').read())
和我的models.py
class FileSaver(models.Model):
myfile = models.FileField(upload_to="files/")
class Meta:
managed=False
预先感谢您的帮助
Django使用它自己的file type(有悦目增强功能)。反正Django的文件类型就像一个decorator,所以你可以简单地将其套在现有的文件对象,以满足Django的API的需求。
from django.core.files import File
local_file = open('mytest.pdf')
djangofile = File(local_file)
pdfImage.myfile.save('new', djangofile)
local_file.close()
当然你也可以通过编写以下(一条线以下)装饰上飞文件:
pdfImage.myfile.save('new', File(local_file))