Storing file content in DB

前端 未结 4 989
温柔的废话
温柔的废话 2021-02-14 04:08

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?

4条回答
  •  眼角桃花
    2021-02-14 05:06

    it is very easy

    just overide save method in 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()
    

    this will store filecontent in db column filecontent of table filecontent

提交回复
热议问题