You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将用户上传图片转换的PDF存入Django的ImageField字段

问题根因

  1. 你遇到的AttributeError: 'str' object has no attribute 'file'报错,是因为FileSystemStorage.save()方法返回值为保存成功后的文件名字符串,并非文件对象,调用.file属性自然会失败。
  2. 直接传thumb_file数据库为空的原因是,Django的ImageField/FileField接收内存文件对象时,必须指定文件名,否则无法完成存储逻辑,导致字段为空。

解决方案

你有两种修改方式可选,推荐第二种更符合Django的设计规范:

方案1:直接使用你已保存的文件路径

你已经通过fs.save()将PDF存到了本地,直接将返回的文件名传给link字段即可:

# 原有代码不变
filename = fs.save('photo.pdf', thumb_file)
# 去掉.file,直接传filename字符串
linkobj = Link(link = filename, person = Section.objects.get(section_name = section), date = str(datetime.date.today()), time = datetime.datetime.now().strftime('%H:%M:%S'))
linkobj.save()

方案2:通过模型字段自带的方法存储(推荐)

不需要手动实例化FileSystemStorage,直接用模型字段的save方法处理存储,自动匹配你models.py中配置的upload_to='images'路径,还能自动处理文件名冲突:

def formsubmit(request):                        
    docs = request.FILES.getlist('photos')
    section = request.POST['section']
    # 提前查询section,避免循环里重复查库
    section_obj = Section.objects.get(section_name=section)
    for x in docs:
        img = Image.open(io.BytesIO(x.read()))
        imgc = img.convert('RGB')
        pdfdata = io.BytesIO()
        imgc.save(pdfdata, format='PDF')
        thumb_file = ContentFile(pdfdata.getvalue())
        # 生成不重复的文件名,避免覆盖
        file_name = x.name.rsplit('.', 1)[0] + '.pdf'
        # 先实例化模型
        linkobj = Link(
            person = section_obj,
            date = str(datetime.date.today()),
            time = datetime.datetime.now().strftime('%H:%M:%S')
        )
        # 用字段的save方法存文件,save=False避免重复提交
        linkobj.link.save(file_name, thumb_file, save=False)
        linkobj.save()
    return redirect('index')

额外优化建议

  • 建议将models.py中的link字段从ImageField改为FileField:ImageField仅用于存储图片格式,内置了图片格式校验逻辑,存储PDF会有兼容性问题,FileField用法完全一致,更适合存储PDF文件。
  • 你当前的date、time字段用CharField存储不合理,建议替换为Django原生的DateField、TimeField,不需要手动转字符串,后续查询、排序效率更高。
  • 现有代码保存的文件名固定为photo.pdf,多次上传会覆盖之前的文件,建议按方案2的逻辑生成唯一文件名。

内容的提问来源于stack exchange,提问作者tyzion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 08:27:01