如何在odoo中将附件文件转换为其他格式?

问题描述

如何在odoo中的附件上传添加观察者以检查某种文件类型并对该文件进行操作?例如,在上传时转换为另一种格式。

编辑

在我的代码修改过的

values变量在addons/web/controllers/main.py:upload_attachment()中仍然无效,因为那里的文件存储变量仍然是未修改的旧版本。

EDIT2

在这里,我添加了尝试更改自定义功能中上载的文件名称时的错误。因为在方法addons/web/controllers/main.py:upload_attachment()中,旧的上传文件名仍然存在。

ERROR odoo13 odoo.addons.web.controllers.main: Fail to upload attachment docum2.doc 
Traceback (most recent call last):
  File "/home/computer/13-ver-odoo/addons/web/controllers/main.py",line 1512,in upload_attachment
    attachment = Model.create({
  File "<decorator-gen-94>",line 2,in create
  File "/home/computer/13-ver-odoo/odoo/api.py",line 314,in _model_create_single
    return create(self,arg)
  File "/home/computer/13-ver-odoo/local-addons/crm_checklist/models/ir_attachment.py",line 54,in create
    return super(IrAttachment,self).create(values)
  File "<decorator-gen-39>",line 335,in _model_create_multi
    return create(self,[arg])
  File "/home/computer/13-ver-odoo/odoo/addons/base/models/ir_attachment.py",line 515,in create
    values.update(self._get_datas_related_values(values.pop('datas'),values['mimetype']))
  File "/home/computer/13-ver-odoo/odoo/addons/base/models/ir_attachment.py",line 212,in _get_datas_related_values
    bin_data = base64.b64decode(data) if data else b''
  File "/usr/lib/python3.8/base64.py",line 80,in b64decode
    s = _bytes_from_decode_data(s)
  File "/usr/lib/python3.8/base64.py",line 45,in _bytes_from_decode_data
    raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like object or ASCII string,not 'tuple'

解决方法

似乎可以改写模型的write / create函数。但是,这里提供的参考是它如何处理“图像”字段。

https://github.com/odoo/odoo/blob/edacc0fc920e27b9759408887762fcba284ee391/odoo/fields.py#L2079

,

文件上传由网络客户端处理,并使用javascript编写。

要在后端(Python)中转换文件,需要先将文件转换为写入数据库,然后再覆盖create/write方法。

示例:使用ir.attachment模型和datas二进制字段

class IrAttachment(models.Model):
    _inherit = 'ir.attachment'

    def process_attachment(self,values):
        pass

    @api.model
    def create(self,values):
        self.process_attachment(values)
        return super(IrAttachment,self).create(values)

    @api.multi
    def write(self,self).write(values)