跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. mass editing 如何提高速度

mass editing 如何提高速度

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
4 帖子 3 发布者 2.3k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • H 离线
    H 离线
    hui
    写于 最后由 编辑
    #1

    大家好,

    我重写的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
    
    1 条回复 最后回复
    0
    • JoshuaJ 离线
      JoshuaJ 离线
      Joshua 管理员
      写于 最后由 编辑
      #2

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

      【上海先安科技】(joshua AT openerp.cn),欢迎关注公众号:openerp_cn

      H 1 条回复 最后回复
      0
      • H 离线
        H 离线
        hui
        在 回复了 Joshua 最后由 编辑
        #3

        @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')
        
        digitalsatoriD 1 条回复 最后回复
        0
        • digitalsatoriD 离线
          digitalsatoriD 离线
          digitalsatori 管理员
          在 回复了 hui 最后由 编辑
          #4

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

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

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

          【上海先安科技】(tony AT openerp.cn)

          1 条回复 最后回复
          0

          • 登录

          • 没有帐号? 注册

          • 登录或注册以进行搜索。
          • 第一个帖子
            最后一个帖子
          0
          • 版块
          • 标签
          • 热门
          • 用户
          • 群组