1.models.py
# 上传文件字段,要继承ir.attachment,且多对多,并在视图中加上widget="many2many_binary"。 udate_file = fields.Many2many(‘ir.attachment‘, string=u‘上传附件‘) udate_file_dir = fields.Char(string="附件路径", compute="copy_file", store=True) demo_order_name = fields.Char(string="附件名称", compute="_value_pc", store=True) # 获取附件名称 @api.depends(‘udate_file‘) def _value_pc(self): for record in self: for d in self.udate_file: record.demo_order_name = d.name # 将上传的文件保存到本地 @api.depends(‘udate_file‘) def copy_file(self): # 如果上传文件对象集合有内容 if self.udate_file: # 循环文件对象集合 for d in self.udate_file: # 转换格式 content_base64 = base64.b64decode(d.datas) dir = os.getcwd() # 写入文件 with open(dir+‘/static/‘ + d.name, ‘wb‘) as f: f.write(content_base64) # 保存附件路径 for i in self: i.udate_file_dir = dir+‘/static/‘ + d.name
2.views.xml
<field name="udate_file" widget="many2many_binary"/> <field name="demo_order_name"/>