pythonpython-2.7odooodoo-10odoo-11

Odoo Error, a partner cannot follow twice the same object


This is the error I'm getting I have receiving this odoo error when I try to duplicate a set of record. I have inherited ['mail.thread', 'ir.needaction_mixin'] in the current class. I didn't found any solution online or myself or from odoo. Still stuck here around four days.

Can anybody have an idea on this? Currently I'm using Odoo 10.

Code added is below:

@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

Solution

  • You should use mail.threads built-in functions to add followers. You should always avoid the use of direct queries!

    Following code will cover the follower and subtype adding (i didn't understand the name writes at the end):

    @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