mass editing 如何提高速度
-
大家好,
我重写的
write()方法
,实现的功能有,三个部门的owner变化了要自动添加关注者,同时发送消息;如果state变化也要发送消息的。问题:
mass editing的时候,最多修改120条数据,再多的话就会报错:CPU time limit exceeded.
怎么提高效率呢?@api.multi def write(self, vals): base_url = self.sudo().env['ir.config_parameter'].get_param('web.base.url') action = self.env.ref('sps_product.product_template_action_all') new_owner_dfs_id = vals.get('owner_dfs') new_owner_sourcing_id = vals.get('owner_sourcing') new_owner_planning_id = vals.get('owner_planning') state = vals.get('status', False) for rec in self: receivers = (rec.owner_dfs | rec.owner_sourcing | rec.owner_planning | rec.owner_purchaser) - rec.env.user old_owner_dfs_id = rec.owner_dfs.id old_owner_sourcing_id = rec.owner_sourcing.id old_owner_planning_id = rec.owner_planning.id ctx = {} result = super(ProductTemplate, rec).write(vals) # ctx.update({'mail_create_nosubscribe': True}) if new_owner_dfs_id and old_owner_dfs_id != new_owner_dfs_id: rec.message_subscribe_users(user_ids=new_owner_dfs_id, subtype_ids=None) receiver = self.env['res.users'].browse(new_owner_dfs_id) ctx.update({'section': 'DFS', 'receiver': receiver, 'from_who': rec.env.user}) rec.with_context(ctx).send_assign_msg() # if new_owner_sourcing_id and old_owner_sourcing_id != new_owner_sourcing_id: rec.message_subscribe_users(user_ids=new_owner_sourcing_id, subtype_ids=None) receiver = self.env['res.users'].browse(new_owner_sourcing_id) ctx.update({'section': 'Sourcing', 'receiver': receiver, 'from_who': rec.env.user}) rec.with_context(ctx).send_assign_msg() if new_owner_planning_id and old_owner_planning_id != new_owner_planning_id: rec.message_subscribe_users(user_ids=new_owner_planning_id, subtype_ids=None) receiver = self.env['res.users'].browse(new_owner_planning_id) ctx.update({'section': 'Planning', 'receiver': receiver, 'from_who': rec.env.user}) rec.with_context(ctx).send_assign_msg() if state: ctx.update(section='', receiver=receivers, old_status=rec.state, status=state, base_url=base_url, action=action, today=fields.Date.today()) msg = Template(status_update_notification_msg).render(object=rec, ctx=ctx) subject = Template(status_update_notification_sub).render(object=rec, ctx=ctx) rec.message_subscribe_users(user_ids=receivers.mapped('id'), subtype_ids=None) rec.message_post(body=msg, subject=subject, message_type='comment', subtype='mt_comment', content_subtype='html') return result
-
@joshua 在 mass editing 如何提高速度 中说:
send_assign_msg
下面就是这个方法,应该不是吧,感觉像是write方法中的循环导致的。
def send_assign_msg(self): context = dict(self._context or {}) receiver = context.get('receiver', False) section = context.get('section', False) from_who = context.get('from_who', False) base_url = self.sudo().env['ir.config_parameter'].get_param('web.base.url') action = self.env.ref('sps_product.product_template_action_all') ctx = {} ctx.update(from_who=from_who, receiver=receiver, section=section, base_url=base_url, action=action) msg = Template(part_assign_msg).render(object=self, ctx=ctx) subject = Template(part_assign_sub).render(object=self, ctx=ctx) self.message_post(body=msg, subject=subject, message_type='comment', subtype='mt_comment', content_subtype='html')
-