Odoo错误,伙伴不能关注同一个对象两次
Odoo Error, a partner cannot follow twice the same object
当我尝试复制一组记录时收到此 odoo 错误。我在现在的class继承了['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
提前致谢和问候。
您应该使用 mail.thread
的内置函数来添加关注者。您应该始终避免使用直接查询!
下面的代码将涵盖follower和子类型的添加(我没看懂最后写的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
您可以使用此代码:
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)
这个错误是因为 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,
})
['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
提前致谢和问候。
您应该使用 mail.thread
的内置函数来添加关注者。您应该始终避免使用直接查询!
下面的代码将涵盖follower和子类型的添加(我没看懂最后写的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
您可以使用此代码:
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)
这个错误是因为 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,
})