我想从表 target_product_line 获取数据,并将其保存在草稿中时将其存储在表 week_planning_line 中。所以就做成这样了
class weekly_planning(models.Model):
_name='weekly.planning'
@api.multi
def action_draft(self):
self.state='draft'
res = super(weekly_planning,self).action_draft()
weekly_planning = self.env['weekly.planning.line']
self.env.cr.execute('''
Select tpl.customer_id
FROM sale_target_detail std
LEFT JOIN target_product_line tpl on tpl.target_product_id = std.id
WHERE std.sales_id = %s
AND EXTRACT(YEAR FROM std.target_date) = %s
GROUP BY tpl.customer_id
''',(self.user_id.id, self.year_period))
for rec in self.env.cr.dictfetchall():
weekly_planning.create({'weekly_planning_id':self.id,
'customer_id':rec['customer_id'],
})
return res
没有错误,但没有客户插入表weekly_planning_line。 请帮忙。谢谢
我认为我在草稿中添加脚本是错误的,所以我决定将脚本放在确认部分并进行一些更新,并且它有效
@api.multi
def action_confirm(self):
self.state='confirm'
self.env.cr.execute("""
Select tpl.customer_id
FROM sale_target_detail std
LEFT JOIN target_product_line tpl on tpl.target_product_id = std.id
WHERE std.sales_id = %s
AND EXTRACT(YEAR FROM std.target_date) = %s
GROUP BY tpl.customer_id
"""
,(self.user_id.id, self.year_period))
for x in self.env.cr.dictfetchall():
profit_line_obj = self.env['weekly.planning.line']
profit_line_obj.create({'weekly_planning_id':self.id,
'customer_id':x['customer_id'],
})