In order to have more flexible file serving functionality within Odoo, you can swap out binary fields for a functional field that returns the contents of your file. Below is a sample class that loads a file’s contents from a file system path and serves it to the user using a functional field. It was originally built for version 6.0 but should work on newer versions too:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
from osv import osv, fields | |
class my_class(osv.osv_memory): | |
def get_file(self, cr, uid, ids, field_name=None, arg=None, context=None): | |
result = dict.fromkeys(ids) | |
for record_browse in self.browse(cr, uid, ids): | |
f = open(record_browse.file_path) | |
result[record_browse.id] = base64.encodestring(f.read()) | |
f.close() | |
return result | |
_name = 'my.class' | |
_columns = { | |
'file_path': fields.char('File Location', size=128), | |
'file': fields.function(get_file, method=True, store=False, type='binary', string="Download File"), | |
} | |
my_class() |