将文件内容存储在数据库中 [英] Storing file content in DB

查看:108
本文介绍了将文件内容存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个模型,其中我有一个 FileField 。我想将文件内容存储在数据库列中,而不是文件路径。任何建议?

I am making a model in which i have a FileField. I want to store the file content in a database column, instead of file path. Any suggestions?

推荐答案

这很简单

方法在admin

filecontent=form.cleaned_data.get('upload_file')  
data =filecontent.read()
from django.db import connection
cursor = connection.cursor()
cursor.execute("update filecontent set filecontent=(%s) where id=(%s)",[data,obj.id])
connection.connection.commit()
cursor.close()
connection.close()

这将把filecontent存储在table filecontent的db column filecontent中

this will store filecontent in db column filecontent of table filecontent

这篇关于将文件内容存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆