djangodjango-admindjango-forms

Storing file content in DB


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?


Solution

  • 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