from odoo import models, fields, api, _ class IrAttachmentWizard(models.TransientModel): _name = 'ir.attachment.wizard' _description = '文件上传向导' attachment = fields.Binary(string='选择文件', required=True) filename = fields.Char(string='文件名') res_model = fields.Char() res_id = fields.Integer() # def action_upload_file(self): # self.ensure_one() # # 首先创建 ir.attachment # attachment = self.env['ir.attachment'].create({ # 'name': self.filename, # 'type': 'binary', # 'datas': self.attachment, # 'res_model': self.res_model, # 'res_id': self.res_id, # }) # # # 获取默认的文档文件夹 # workspace = self.env['documents.folder'].search([('name', '=', '采购合同')], limit=1) # # # 创建 documents.document 记录 # document = self.env['documents.document'].create({ # 'name': self.filename, # 'attachment_id': attachment.id, # 'folder_id': workspace.id, # 'res_model': self.res_model, # 'res_id': self.res_id, # }) # # return { # 'type': 'ir.actions.client', # 'tag': 'display_notification', # 'params': { # 'title': _('成功'), # 'message': _('文件上传成功'), # 'type': 'success', # } # } def action_upload_file(self): self.ensure_one() # 获取当前用户的 partner_id current_partner = self.env.user.partner_id # 首先创建 ir.attachment attachment = self.env['ir.attachment'].create({ 'name': self.filename, 'type': 'binary', 'datas': self.attachment, 'res_model': self.res_model, 'res_id': self.res_id, }) # 获取默认的文档文件夹 workspace = self.env['documents.folder'].search([('name', '=', '采购合同')], limit=1) # 创建 documents.document 记录 document = self.env['documents.document'].create({ 'name': self.filename, 'attachment_id': attachment.id, 'folder_id': workspace.id, 'res_model': self.res_model, 'res_id': self.res_id, 'partner_id': current_partner.id, }) # 更新采购订单的合同文档字段 purchase_order = self.env['purchase.order'].browse(self.res_id) purchase_order.write({ 'contract_document_id': document.id, 'is_upload_contract_file': True }) # 显示成功消息并关闭向导 message = { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('成功'), 'message': _('文件上传成功'), 'type': 'success', 'sticky': False, # 自动消失 'next': { 'type': 'ir.actions.act_window_close' } } } return message # def action_upload_file(self): # self.ensure_one() # attachment = self.env['ir.attachment'].create({ # 'name': self.filename, # 'type': 'binary', # 'datas': self.attachment, # 'res_model': self.res_model, # 'res_id': self.res_id, # }) # return { # 'type': 'ir.actions.client', # 'tag': 'display_notification', # 'params': { # 'title': _('成功'), # 'message': _('文件上传成功'), # 'type': 'success', # } # }