如何在odoo中将附件文件转换为不同的格式?
How to convert attachment file to different format in odoo?
如何在odoo中添加附件上传观察者来检查某个文件类型并对该文件进行操作?例如,在上传时转换为另一种格式。
编辑
values
在我的代码中修改的变量在 addons/web/controllers/main.py:upload_attachment()
中仍然没有效果,因为文件存储变量仍然是旧的,未修改的。
编辑2
这里我添加了当我尝试更改自定义函数中上传的文件的名称时的错误。因为在方法中 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 2, in create
File "/home/computer/13-ver-odoo/odoo/api.py", 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
文件上传由web客户端处理,写在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, values):
self.process_attachment(values)
return super(IrAttachment, self).write(values)
如何在odoo中添加附件上传观察者来检查某个文件类型并对该文件进行操作?例如,在上传时转换为另一种格式。
编辑
values
在我的代码中修改的变量在 addons/web/controllers/main.py:upload_attachment()
中仍然没有效果,因为文件存储变量仍然是旧的,未修改的。
编辑2
这里我添加了当我尝试更改自定义函数中上传的文件的名称时的错误。因为在方法中 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 2, in create
File "/home/computer/13-ver-odoo/odoo/api.py", 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
文件上传由web客户端处理,写在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, values):
self.process_attachment(values)
return super(IrAttachment, self).write(values)