Odoo13_上傳檔案並指定儲存到位置
阿新 • • 發佈:2020-09-19
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"/>