【问题标题】:How to convert attachment file to different format in odoo?如何在odoo中将附件文件转换为不同的格式?
【发布时间】:2020-08-17 10:00:26
【问题描述】:

如何在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 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'

【问题讨论】:

  • 要检查上传的文件,您可以扩展二进制小部件并覆盖on_file_uploaded_and_valid(size, name, content_type, file_base64) 方法
  • 感谢@Kenly 的回复,但我需要在后端用python 代码检查一下,以便立即转换文件。
  • 您可以在将文件写入数据库之前对其进行转换,请在下面查看我的答案。

标签: python odoo odoo-12 odoo-13


【解决方案1】:

似乎覆盖模型的write/create 函数是可行的方法。但是这里的参考是它是如何为 Image 字段完成的。

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

【讨论】:

  • 谢谢你,你的回答也很有用,可惜我不能接受两个正确的答案,因为它们都有帮助。
【解决方案2】:

文件上传由网络客户端处理,是用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)

【讨论】:

  • 它可以工作,但是无论我在覆盖的函数中所做的更改,对加载在 addons/web/controllers/main.py:upload_attachment() 中的文件都没有影响,我已经编辑了问题
  • 如果我理解的话,问题是保存在数据库中的值被转换了,但是 WebClient 中显示的附件没有改变(它仍然是你上传的文件)?
  • 它不显示 Web 客户端中的文件,因为某处有错误。在 main.py 文件有它的旧名称,但我在我的模块中更改了它。我想这就是发生错误的原因。
  • 请编辑您的问题并添加最后的错误日志行。
  • 在问题中添加了 EDIT 2
猜你喜欢
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 2021-12-08
  • 2017-08-22
相关资源
最近更新 更多