在 django admin 中上传后文件为空

问题描述 投票:0回答:1

我想在模型的保存方法中将预先创建的 pdf 文件保存到我的 django 模型中:

class MyModel(BaseModel):    
    pdf_file = models.FileField(upload_to='pdfs',blank=True, null=True)

还有我的 mdoel 保存:

def save(self, *args, **kwargs):

     #create the file at pdf_path with fpdf

     self.pdf_file.save("myfile.pdf" , open(pdf_path, "r", encoding='ISO8859-2'), save=False)  
     super().save(*args, **kwargs)

没有错误,文件是在 pdf_path 创建的,是一个包含内容的 5 页文件。还有一个文件上传到 django 数据库中,但它是一个空的 5 页 pdf 文件。

django django-admin
1个回答
-1
投票

如果您使用 PySPDF 或其他库,您也可能必须标记它。
抱歉,我无法为您提供更多帮助:(

© www.soinside.com 2019 - 2024. All rights reserved.