我正在尝试根据crm.lead中的输入字段(product_quantity)创建多个销售订单。
@api.multi
def create_sale(self):
values = {
'opportunity_id': self.id,
'state': 'draft',
'date_order': datetime.now(),
'partner_id': self.partner_id.id
}
print '###############', self.product_quantity
for i in range(0, self.product_quantity):
print '$$$$$$$$$$$$$$$$'
res = self.env['sale.order'].create(values)
print res
我收到以下错误弹出
Error, a partner cannot follow twice the same object.
答案 0 :(得分:2)
@Khelili,感谢您的分享。添加上下文对我来说很好。我通过谷歌搜索找到了它。
for i in range(0, self.product_quantity):
values = {
'name': self.env['ir.sequence'].next_by_code('sale.order'),
'opportunity_id': self.id,
'state': 'draft',
'date_order': datetime.now(),
'partner_id': self.partner_id.id,
'count_total': self.total_count,
'product_categ': self.product_categ.id
}
res = self.env['sale.order'].with_context(mail_create_nosubscribe=True).create(values)
答案 1 :(得分:1)
我遇到了同样的问题,经过深入搜索,我发现邮件模块addons/mail/models/mail_followers.py
中有一个唯一约束。
_sql_constraints = [
source of problem =====> ('mail_followers_res_partner_res_model_id_uniq', 'unique(res_model,res_id,partner_id)', 'Error, a partner cannot follow twice the same object.'),
('mail_followers_res_channel_res_model_id_uniq', 'unique(res_model,res_id,channel_id)', 'Error, a channel cannot follow twice the same object.'),
('partner_xor_channel', 'CHECK((partner_id IS NULL) != (channel_id IS NULL))', 'Error: A follower must be either a partner or a channel (but not both).')
]
然后,在您的情况下,我使用SQL查询创建了记录:
@api.multi
def create_sale(self):
opportunity_id: self.id,
state: 'draft',
date_order: datetime.now(),
partner_id: self.partner_id.id
current_uid = self.env.user.id
print '###############', self.product_quantity
for i in range(0, self.product_quantity):
print '$$$$$$$$$$$$$$$$'
res = self.env['sale.order'].create(values)
result = self.env.cr.execute(("""INSERT INTO sale_order"""
""" (opportunity_id, state, date_order, partner_id, create_date, create_uid)"""
""" VALUES (%s, '%s', '%s', %s, '%s', %s)""")% (
opportunity_id, state, date_order, partner_id, date_order, current_uid))
答案 2 :(得分:0)
就我而言,我的模型有一个名为“channel_id”的字段,该字段与关注者的 channel_id 混淆。我花了一段时间才弄明白。