问题描述
def action_send_card(self):
template_id = self.env.ref('library.agreement_email_template').id
data_id = self.env['ir.attachment'].browse(self.agreement_file.id)
template = self.env['mail.template'].browse(template_id)
for pdf in data_id:
template.attachment_ids = [(3,pdf.id)]
template.attachment_ids = [(6,[pdf.id])]
template.send_mail(self.id,force_send=True)
return True
我用一个文件试过,效果很好,但是当我尝试两个或更多文件时,它给了我同样的错误。
ValueError:预期的单例:ir.attachment(260,257)
协议文件声明如下:
agreement_file = fields.Many2many(
'ir.attachment','class_ir_attachments_rel','class_id','attachment_id',string="Agreement files",required=False)
解决方法
self
可能包含多个记录集和需要单个记录的方法。
尝试使用以下代码:
def action_send_card(self):
template_id = self.env.ref('library.agreement_email_template').id
for record in self:
data_id = self.env['ir.attachment'].browse(record.agreement_file.id)
template = self.env['mail.template'].browse(template_id)
for pdf in data_id:
template.attachment_ids = [(3,pdf.id)]
template.attachment_ids = [(6,[pdf.id])]
template.send_mail(record.id,force_send=True)
return True
有关此问题的详细信息:https://odedrabhavesh.blogspot.com/2017/02/valueerror-expected-singleton-in-odoo.html
,agreement_file
是 amny2many,对于您的特定情况,分配了多个 ir.attachment
记录。第 3 行 data_id = self.env['ir.attachment'].browse(self.agreement_file.id)
出现问题,因为 agreement_file
是多条记录的记录集,您必须访问 ids
而不是 id
。
def action_send_card(self):
template_id = self.env.ref('library.agreement_email_template').id
data_id = self.env['ir.attachment'].browse(self.agreement_file.ids)
template = self.env['mail.template'].browse(template_id)
for pdf in data_id:
template.attachment_ids = [(3,pdf.id)]
template.attachment_ids = [(6,[pdf.id])]
template.send_mail(self.id,force_send=True)
return True
或者更简单
def action_send_card(self):
template = self.env.ref('library.agreement_email_template')
template.attachment_ids = [(6,self.agreement_file.ids)]
template.send_mail(self.id,force_send=True)
return True
,
我在这里尝试了一些解决方案,但仍然没有用。我更改了代码中的某些内容,并以某种方式找到了解决方案。在这里
echo %username%