文件更新

django 文件更新:

文件更新一定要放到最后,否则文件会丢失!

参考https://blog.csdn.net/weixin_33127753/article/details/88227832

from django.core.files.base import ContentFile

file = request.FILES.get("wenjian", None)
if not file:
return HttpResponse("没有文件!")
if file:
BookInfo.objects.filter(id=9).update(**real_dict)


obj = BookInfo.objects.filter(id=9).first() # 获取对应类的实例化对象
file_content = ContentFile(file.read()) # 创建ContentFile对象
obj.wenjian.save(file.name,file_content)
obj.save()

原文地址:https://www.cnblogs.com/realadmin/p/11955165.html