Get images from db by fields.function on QWeb report - Odoo 8

泪湿孤枕 提交于 2019-12-23 01:37:50

问题


I want to print an image getting it by a fields.function for any image in my database.

I am trying the following:

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"),
}

but in qweb report I got:

  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

If I print the image in a form, everything is going well.

Any help would be appreciated, thanks in advance.


回答1:


Try this code:

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"),

}



回答2:


Or try This code

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


来源:https://stackoverflow.com/questions/34026022/get-images-from-db-by-fields-function-on-qweb-report-odoo-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!