Reputation: 685
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
Upvotes: 2
Views: 3011
Reputation: 3822
This error is because an 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.'), ...
Located in /addons/mail/models/mail_followers.py
, when you create an object the current user added automatically as follower twice in this models ['mail.followers']
, so the sql_constraint will be : Unique('your_model', id_of_record_you_want_to_create, current_user)
twice.
Solution : you can use option .with_context(mail_create_nosubscribe=True)
and this means do not add automatically the current user as followers.
Exemple :
self.env['helpdesk.ticket'].with_context(mail_create_nosubscribe=True).create({
'name' : 'ticket_name',
'partner_id' : 22,
'team_id' : 2,
})
Upvotes: 0
Reputation: 325
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)
Upvotes: 1
Reputation: 14778
You should use mail.thread
s 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
Upvotes: 3