Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

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

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

    mass editing 如何提高速度

    Odoo 开发与实施交流
    3
    4
    1990
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hui last edited by

      大家好,

      我重写的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 Reply Last reply Reply Quote 0
      • Joshua
        Joshua 管理员 last edited by

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

        H 1 Reply Last reply Reply Quote 0
        • H
          hui @Joshua last edited by

          @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')
          
          digitalsatori 1 Reply Last reply Reply Quote 0
          • digitalsatori
            digitalsatori 管理员 @hui last edited by

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

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

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post