【问题标题】:odoo-12 : How to solve the write() method problemodoo-12 : 如何解决 write() 方法问题
【发布时间】:2019-10-31 11:12:09
【问题描述】:

在我的情况下,我继承了 crm 表单并添加了一个 mamy2many 字段,此名称 [Estimation Assign to] 当我从该字段中选择用户并保存在添加关注者中添加所选用户并发送邮件的时间记录。现在的问题是,当我将看板卡从一个阶段更改为另一个阶段时,如果在表单估计字段中分配了一些用户,邮件也会发送这些用户。

但我只想在我打开记录并从估计字段中选择用户然后单击保存按钮时才发送邮件,我只想发送邮件。不是当我将看板卡从一个阶段更改为另一个阶段时。

如果你知道,请告诉我。

@api.model
def create(self, vals):
    lead_res = super(CrmLead, self).create(vals)
    for rec in lead_res:
        if rec.estimation_id:
            partner_ids = []
            for est_rec in rec.estimation_id:
                if est_rec.partner_id and est_rec.partner_id.email:
                    user_name = self.env.user.name_get()[0][1]
                    partner_ids.append(est_rec.partner_id.id)
                    template_obj = self.env['mail.mail']
                    template_data = {
                                    'subject': 'New Estimation Asign : ',
                                    'body_html': "Hello,</br><h5>" + user_name + " invited you to follow Lead/Opportunity document : " + rec.name + "</h5>",
                                    'email_from': self.env['mail.message']._get_default_from(),
                                    'email_to': est_rec.partner_id.email
                                    }
                    template_id = template_obj.create(template_data)
                    template_obj.send(template_id)
            if partner_ids:
                rec.message_subscribe(partner_ids, None)
    return lead_res

@api.multi
def write(self, vals):
    res = super(CrmLead, self).write(vals)
    for rec in self:
        if rec.estimation_id:
            partner_ids = []
            for est_rec in rec.estimation_id:
                if est_rec.partner_id and est_rec.partner_id.email:
                    user_name = self.env.user.name_get()[0][1]
                    partner_ids.append(est_rec.partner_id.id)
                    template_obj = self.env['mail.mail']
                    template_data = {
                                    'subject': 'New Estimation Asign : ',
                                    'body_html': "Hello,</br><h5>" + user_name + " invited you to follow Lead/Opportunity document : " + rec.name + "</h5>",
                                    'email_from': self.env['mail.message']._get_default_from(),
                                    'email_to': est_rec.partner_id.email
                                }
                    template_id = template_obj.create(template_data)
                    print('===================To sent ==================', est_rec.partner_id.email)
                    template_obj.send(template_id)
                    rec.message_subscribe(partner_ids, None)
                    #message_unsubscribe
                    message_partner_ids = rec.message_partner_ids.ids
                    est_ids = [est_rec.partner_id.id for est_rec in rec.estimation_id] + [self.env.ref('base.partner_root').id]
                    unsub_partners = set(message_partner_ids) - set(est_ids)

                    if list(unsub_partners):
                        rec.message_unsubscribe(list(unsub_partners))
        else:
            print("+++++============= Else Part =============+++++")
    return res

【问题讨论】:

    标签: python odoo sendmail odoo-12 kanban


    【解决方案1】:

    estimation_id发生变化时,尝试添加另一个发送邮件的条件。

    if u'estimation_id' in vals and rec.estimation_id:
    

    编辑
    以下代码将计算新添加的用户:

    user_ids = {rec.id: [user_id.id for user_id in rec.estimation_id] for rec in self}
    res = super(CrmLead, self).write(vals)
    for rec in self:
        new_user_ids = [user.id for user in rec.estimation_id if user.id not in user_ids[rec.id]]
    

    【讨论】:

    • 现在最后一个问题是,当我们当时更新记录时,假设在您保存当时的记录后,当我们从estimate_id 的下拉列表中删除一个用户时,在estimate_id 字段中已经选择了两个用户发送邮件给剩下的用户。但我不需要再次将邮件发送到已存储在estimate_id 字段中的用户。如果你知道如何解决这个问题,请告诉我。
    • 尝试在调用super之前获取用户,并在调用后进行比较以获得新的注册用户。
    • 您好,请您编辑我的代码并给我解决方案。因为我不明白如何在调用 super 之前获取用户。感谢高级
    • 我编辑了我的代码并添加了一个示例代码来存储用户 ID,然后调用 super 将它们与 estimation_id 进行比较以获取新用户
    • 您好,我使用了您编辑的代码,但无法获取所选用户的电子邮件 ID。如果您知道如何获取选定用户的电子邮件 ID。告诉我怎么做。
    猜你喜欢
    • 2019-05-18
    • 2021-09-06
    • 1970-01-01
    • 2017-11-15
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多