【问题标题】:Get images from db by fields.function on QWeb report - Odoo 8通过 QWeb 报告上的 fields.function 从 db 获取图像 - Odoo 8
【发布时间】:2015-12-01 17:10:08
【问题描述】:

我想为我的数据库中的任何图像打印一个通过 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

如果我以表格形式打印图像,一切都会顺利。

任何帮助将不胜感激,在此先感谢。

【问题讨论】:

    标签: python report openerp odoo-8 qweb


    【解决方案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"),
    
    }
    

    【讨论】:

    • 找到正确的图像编码方式 res[record_browse.id] = partner.read().encode('base64')
    • res[record_browse.id] = base64.encodestring(partner) 此代码在 odoo 中工作
    【解决方案2】:

    或者试试这个代码

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

    【讨论】:

    • 找到正确的图像编码方式 res[record_browse.id] = partner.read().encode('base64')
    • result[record_browse.id] = base64.encodestring(f.read()) 此代码在 python 和 odoo 中工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多