Odoo 中文社区

    • 注册
    • 登录
    • 搜索
    • 版块
    • 标签
    • 热门
    • 用户
    • 群组

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

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

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

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

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

    [已解决]inverse 方法没作用

    Odoo 开发与实施交流
    2
    4
    640
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • H
      hui 最后由 hui 编辑

      如下代码,有个计算字段,我希望同时可以编辑,我写了一个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
      • digitalsatori
        digitalsatori 管理员 最后由 digitalsatori 编辑

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

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

        H 2 条回复 最后回复 回复 引用 0
        • H
          hui @digitalsatori 最后由 编辑

          @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
            hui @digitalsatori 最后由 hui 编辑

            @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
            • First post
              Last post