Odoo错误,伙伴不能跟随同一对象两次

时间:2018-12-07 10:10:56

标签: python python-2.7 odoo odoo-10 odoo-11

This is the error I'm getting 当我尝试复制一组记录时,我收到了这个odoo错误。我已经在当前课程中继承了['mail.thread', 'ir.needaction_mixin']。我没有找到任何在线解决方案,无论是我本人还是odoo。仍然停留在这里约四天。

有人对此有想法吗?目前,我正在使用Odoo 10。

添加的代码如下:

@api.model
    def create(self, vals):
        res = super(StaffKPI, self).create(vals)

        fol = {}
        fol['res_model'] = 'staff.kpi'
        fol['res_id'] = res.id
        fol['partner_id'] = res.name_id.partner_id.id
        fol_id = self.env['mail.followers'].create(fol)
        self._cr.execute(
            'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
            (fol_id.id, 2))
        self._cr.execute(
            'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
            (fol_id.id, 1))

        subtypes = self.env['mail.message.subtype'].search([('res_model', '=', 'staff.kpi')]).ids
        if subtypes:
            for i in subtypes:
                self._cr.execute(
                    'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
                    (fol_id.id, i))
        old_name = res.name
        res.write({'name': ''})
        res.write({'name': old_name})
        return res

预先感谢和问候。

3 个答案:

答案 0 :(得分:3)

您应该使用mail.thread的内置函数添加关注者。您应该始终避免使用直接查询!

以下代码将覆盖跟随者和子类型的添加(我不理解最后的name内容):

@api.model
def create(self, vals):
    res = super(StaffKPI, self).create(vals)
    subtype_ids = self.env['mail.message.subtype'].search(
        [('res_model', '=', 'staff.kpi')]).ids
    res.message_subscribe(
        partner_ids=[res.name_id.partner_id.id],
        subtype_ids=subtype_ids)
    # other logic
    return res

答案 1 :(得分:1)

you can use this code:

class Followers(models.Model):
   _inherit = 'mail.followers'

   @api.model
   def create(self, vals):
        if 'res_model' in vals and 'res_id' in vals and 'partner_id' in vals:
            dups = self.env['mail.followers'].search([('res_model', '=',vals.get('res_model')),
                                           ('res_id', '=', vals.get('res_id')),
                                           ('partner_id', '=', vals.get('partner_id'))])
            if len(dups):
                for p in dups:
                    p.unlink()
        return super(Followers, self).create(vals)

答案 2 :(得分:0)

这个错误是因为 SQL_constraint

('mail_followers_res_partner_res_model_id_uniq', 'unique(res_model,res_id,partner_id)', 'Error, a partner cannot follow twice the same object.'), ...

位于/addons/mail/models/mail_followers.py中,当您创建一个对象时,当前用户在此模型['mail.followers']中自动添加为关注者两次,因此sql_constraint将是:Unique('your_model', id_of_record_you_want_to_create, current_user)两次。

解决方案:您可以使用选项 .with_context(mail_create_nosubscribe=True),这意味着不要自动将当前用户添加为关注者。

示例:

self.env['helpdesk.ticket'].with_context(mail_create_nosubscribe=True).create({
   'name' : 'ticket_name',
   'partner_id' : 22,
   'team_id' : 2,
})