在QWeb报告 - Odoo 8上获取db by fields.function的图像

时间:2015-12-01 17:10:09

标签: python report openerp odoo-8 qweb

我想打印一张图片,通过我的数据库中任何图像的fields.function获取它。

我正在尝试以下方法:

def _get_image(self, cr, uid, ids, name, args, context=None):
    res = dict.fromkeys(ids)
    for record_browse in self.browse(cr, uid, ids):
        partner = self.pool.get('res.partner').browse(cr,uid,6,context=None).image
        res[record_browse.id] = base64.encodestring(partner)
    return res   

_columns = {
        'image': fields.function(_get_image, string="Image", type="binary"),
}

但是在qweb报告中我得到了:

  File "/opt/*/openerp/addons/base/ir/ir_qweb.py", line 791, in value_to_html
raise ValueError("Non-image binary fields can not be converted to HTML")
ValueError: Non-image binary fields can not be converted to HTML

如果我在表格中打印图像,一切进展顺利。

任何帮助将不胜感激,提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这段代码:

def _get_image(self, cr, uid, ids, name, args, context=None):
    res = dict.fromkeys(ids)
    for record_browse in self.browse(cr, uid, ids):
        partner = self.pool.get('res.partner').browse(cr,uid,6,context=None).image
        res[record_browse.id] = base64.encodestring(partner)
    return res   

_columns = {
        'image': fields.binary("Image"),
        'image_x': fields.function(_get_image, string="Image", type="binary"),

}

答案 1 :(得分:1)

或尝试此代码

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()