Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

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
    

  • 管理员

    提升效率的办法感觉首先要找到代码里面费时比较多的地方,我猜会不会因为这个方法send_assign_msg



  • @joshuamass 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')
    

  • 管理员

    @1234567mass editing 如何提高速度 中说:

    下面就是这个方法,应该不是吧,感觉像是write方法中的循环导致的。

    发送邮件是一个耗时的操作,感觉应该放到一个线程里。


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待