1、定义bianry字段和下载文件名/按钮名字段
download_button_test = fields.Binary(compute='_compute_download_button_test', string='下载委托测试报告')
download_button_check = fields.Binary(compute='_compute_download_button_check', string='下载委托检验报告')
download_button_check_name = fields.Char(string="委托检验报告名", default='打印委托检验报告')
download_button_test_name = fields.Char(string="委托测试报告名", default='打印委托测试报告')
2、实现binary字段的计算方法,这里是下载的附件
content_path = os.path.join(res_pdf._filestore(), res_pdf.store_fname)
是为了获取附件的真实路径
order.download_button_check = base64.b64encode(open(content_path, ‘rb’).read())
将该附件打开读取后编码再复制给binary字段即可成功
注:这里可以下载任何文件,只需要将要下载的文件的路径赋值给content_path 即可。
@api.model
def _compute_download_button_check(self):
for order in self:
if order.state in ['pz','dy','ff']:
order.download_button_check_name = order.name + '委托检验报告.pdf'
res_pdf = self.env['ir.attachment'].search([('res_model', '=', 'lims.compile.report'), ('res_id', '=', order.id), ('description', '=', '[%s]委托检验报告' % (order.name))])
if res_pdf:
res_pdf = res_pdf[0]
else:
order.download_button_check = False
return False
content_path = os.path.join(res_pdf._filestore(), res_pdf.store_fname)
# content_path = get_module_resource('lims_report', 'static/src/report_pdf', order.name+'委托检验报告.pdf')
if content_path:
order.download_button_check = base64.b64encode(open(content_path, 'rb').read())
else:
order.download_button_check = False
else:
order.download_button_check = False
3、在xml视图中如此写入即可实现该功能
<field name="download_button_check" filename="download_button_check_name" class="btn-primary" style="color: white;height: 25px;" string='下载'/>
<field name="download_button_check_name" invisible="1"/>
<field name="download_button_test" filename="download_button_test_name" class="btn-primary" style="color: white;height: 25px;margin-left:3px" string='下载'/>
<field name="download_button_test_name" invisible="1"/>