【发布时间】:2015-09-28 14:30:16
【问题描述】:
我创建了一个名为 event 的模型和另一个名为 event.participant 的模型。模型event 有字段participants,这是一个指向event.participant 的One2many 字段(event 1:N event.participant)。
event的形式我有一个按钮确认事件,下一个代码:
@api.multi
def confirm_event(self):
for event in self:
return event.participants.notify(composition_mode='mass_mail')
方法notify 是我在模型event.participant 中创建的。它的作用是打开您每次想要从 Odoo 发送邮件时看到的 mail.compose.message 向导表单(带有 from、to、subject 字段、body、template_id 等)。这是notify方法的代码:
@api.multi
def notify(self, composition_mode=False):
form_view_id = self.env.ref(
'mail.email_compose_message_wizard_form').id
ctx = self.env.context.copy()
if composition_mode:
ctx.update({
'default_composition_mode': composition_mode,
})
result = {
'name': 'Send Mail',
'view_type': 'form',
'view_mode': 'form',
'views': [(form_view_id, 'form'), ],
'res_model': 'mail.compose.message',
'context': ctx,
'type': 'ir.actions.act_window',
'target': 'new',
}
return result
现在我将展示我使用此代码得到的行为示例:
我有一个 ID 为 16 的活动。该活动有两个 ID 为 21 和 22 的参与者。我转到活动表单并单击按钮确认活动。发送电子邮件的向导表单已正确打开。但是,然后,我选择了一个电子邮件模板(此电子邮件模板适用于event.participant 模型),然后单击发送邮件。
我收到模板呈现错误。我想我知道为什么会出现这个错误:
向导表单中的active_model 是event,active_ids 是[16],而它们应该是event.participant > 和 [21, 22]。 So, as the email template selected applies to event.participant, the variables inside it fail.例如,电子邮件模板的 to 是${object.email|safe}。 object 变量被 event 的记录 16 替换。模型event 没有名为email 的字段,因此会生成渲染错误。
我无法创建一个适用于event 的模板,因为这将是无稽之谈(我如何在模板的to 中指明所有参与者的电子邮件地址?)。
我必须使用适用于event.participant 的模板,但我需要在渲染时拥有正确的active_model 和active_ids。我尝试直接在上下文中修改这些变量,但又报错了。
我怎样才能达到我的目的?有没有更简单的模式可以做到这一点?任何帮助将不胜感激!
【问题讨论】:
标签: python python-2.7 odoo-8 odoo email-templates