class LastFile(models.Model):
_name = "last_file"
_description = "最新文件"
name = fields.Char("文件标题")
udate_file = fields.Many2many('ir.attachment', string=u'上传附件')
udate_file_dir = fields.Char(string="附件路径")
demo_order_name = fields.Char(string="附件名称")
url = fields.Char(string="本地下载")
@api.onchange('udate_file')
def _onchange_udate_file(self):
if self.udate_file:
# 循环文件对象集合
for d in self.udate_file:
# 转换格式
content_base64 = base64.b64decode(d.datas)
# 当前工作目录路径
dir = os.getcwd()
time_str = str(time.time_ns())
# 拼接路径 + 文件名
path = dir + '/appstore/project/static/document/' + time_str + d.name
if not os.path.exists(path):
# 二进制文件写入
with open(path, mode='wb+') as ff:
ff.write(content_base64)
# 保存附件路径
for i in self:
i.udate_file_dir = path
i.demo_order_name = d.name
# URL保存地址,用于接口直接下载
i.url = '/project/static/document/' + time_str + d.name
标签:DTCloud,name,fields,udate,file,time,附件,path,上传
From: https://www.cnblogs.com/DTCLOUD/p/17144080.html