跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • 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. [已解决]inverse 方法没作用

[已解决]inverse 方法没作用

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

    如下代码,有个计算字段,我希望同时可以编辑,我写了一个inverse方法,但是没效果。

    state_sourcing = fields.Selection([
            ('noaction', 'No Action'),
            ('confirmed', 'To Do'),
            ('progress', 'In Progress'),
            ('done', 'Done')], string='Sourcing Status',
            copy=False, default='confirmed', required=True, store=True, compute='_compute_action_count',
            compute_sudo=True, reverse="_set_state")
    
    @api.multi
        def _set_state(self):
            for rec in self:
               sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state')
              
                if rec.state_sourcing == 'done':
                    sourcing.state = 'done'
                else:
                    pass
                
        @api.multi
        @api.depends('action_ids', 'action_ids.state')
        def _compute_action_count(self):
            for item in self:
                          
                sourcing = item.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state')
                           
                if not sourcing:
                    item.state_sourcing = 'noaction'
                elif 'progress' in sourcing:
                    item.state_sourcing = 'progress'
                elif 'confirmed' in sourcing:
                    item.state_sourcing = 'confirmed'
                else:
                    item.state_sourcing = 'done'
             
    
    1 条回复 最后回复
    0
    • digitalsatoriD 离线
      digitalsatoriD 离线
      digitalsatori 管理员
      写于 最后由 digitalsatori 编辑
      #2

      有点乱啊。你在_set_state中sourcing难道不是字符串列表吗?
      怎么后面还会有sourcing.state,不报错吗?

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

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

        @digitalsatori
        这个的确有问题,不报错是因为这个方法没有执行啊。我改后的如下:

        @api.multi
            def _set_state(self):
                for rec in self:
                   sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing")
                  
                    if rec.state_sourcing == 'done':
                        sourcing.state = 'done'
                    else:
                        pass
                    
            @api.multi
            @api.depends('action_ids', 'action_ids.state')
            def _compute_action_count(self):
                for item in self:
                              
                    sourcing = item.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state')
                               
                    if not sourcing:
                        item.state_sourcing = 'noaction'
                    elif 'progress' in sourcing:
                        item.state_sourcing = 'progress'
                    elif 'confirmed' in sourcing:
                        item.state_sourcing = 'confirmed'
                    else:
                        item.state_sourcing = 'done'
                 
        
        
        1 条回复 最后回复
        0
        • H 离线
          H 离线
          hui
          在 回复了 digitalsatori 最后由 hui 编辑
          #4

          @digitalsatori
          我找到了明显的错误,字段的属性写错了,应该是inverse不是reverse,所以不起作用的。而且inverse对应的方法里面也要修改,代码如下。

          还有个问题就是,感觉inverse有时候起作用,有时候不起作用的。

          其中compute方法是修改了字段立马可以在视图上看见效果的,但是inverse必须保存后才可能看到效果的。

          我改进了inverse对应的方法,让该方法既是inverse的方法,同时也是onchange的方法,让inverse方法修改相关的字段的时候,能里面在视图上看到效果。

          还可以完善的地方就是如果是其他的状态的话,我应该做个限制的。这样就完美了。

          state_sourcing = fields.Selection([
                  ('noaction', 'No Action'),
                  ('confirmed', 'To Do'),
                  ('progress', 'In Progress'),
                  ('done', 'Done')], string='Sourcing Status',
                  copy=False, default='confirmed', required=True, store=True, compute='_compute_action_count',
                  compute_sudo=True, inverse="_set_state")
          
              @api.multi
              @api.onchange('state_dfs', 'state_sourcing', 'state_planning')
              def _set_state(self):
                  for rec in self:
                     sourcing = rec.action_ids.filtered(lambda r: r.type == "sourcing")
                    
                      if rec.state_sourcing == 'done':
                          sourcing.write({'state': 'done'})
                      else:
                          pass
                      
              @api.multi
              @api.depends('action_ids', 'action_ids.state')
              def _compute_action_count(self):
                  for item in self:
                                
                      sourcing = item.action_ids.filtered(lambda r: r.type == "sourcing").mapped('state')
                                 
                      if not sourcing:
                          item.state_sourcing = 'noaction'
                      elif 'progress' in sourcing:
                          item.state_sourcing = 'progress'
                      elif 'confirmed' in sourcing:
                          item.state_sourcing = 'confirmed'
                      else:
                          item.state_sourcing = 'done'
                   
          
          1 条回复 最后回复
          0

          • 登录

          • 没有帐号? 注册

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