放置多个附件时出现 Odoo Singleton 错误
Odoo Singleton error when putting more then one attachment
您好,我想在一封电子邮件中发送多个附件,这是我使用的代码:
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, 0, [pdf.id])]
template.send_mail(self.id, force_send=True)
return True
我用一个文件尝试过,效果很好,但是当我尝试两个或更多文件时,它给了我同样的错误。
ValueError:预期单例:ir.attachment(260, 257)
agreement_file 声明如下:
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, 0, [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, 0, [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, 0, self.agreement_file.ids)]
template.send_mail(self.id, force_send=True)
return True
我尝试了这里的一些解决方案,但仍然没有用。我更改了代码中的某些内容,并以某种方式找到了解决方案。在这里
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 existing_pdf in template.attachment_ids:
template.write({"attachment_ids": [(3, existing_pdf.id)]})
for pdf in data_id:
for new_pdf in pdf:
template.write({"attachment_ids": [(4, new_pdf.id)]})
template.send_mail(self.id, force_send=True)
您好,我想在一封电子邮件中发送多个附件,这是我使用的代码:
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, 0, [pdf.id])]
template.send_mail(self.id, force_send=True)
return True
我用一个文件尝试过,效果很好,但是当我尝试两个或更多文件时,它给了我同样的错误。
ValueError:预期单例:ir.attachment(260, 257)
agreement_file 声明如下:
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, 0, [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, 0, [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, 0, self.agreement_file.ids)]
template.send_mail(self.id, force_send=True)
return True
我尝试了这里的一些解决方案,但仍然没有用。我更改了代码中的某些内容,并以某种方式找到了解决方案。在这里
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 existing_pdf in template.attachment_ids:
template.write({"attachment_ids": [(3, existing_pdf.id)]})
for pdf in data_id:
for new_pdf in pdf:
template.write({"attachment_ids": [(4, new_pdf.id)]})
template.send_mail(self.id, force_send=True)