【问题标题】:Odoo check field in other module其他模块中的 Odoo 检查字段
【发布时间】:2017-04-12 17:33:24
【问题描述】:

如果字段为空,我们如何检查方法。这段代码应该如何正确显示。

我希望能说明我的观点。

def method(self, vals):
    if vals.get("field" == empty)
    use my logic
    if not empty use data that was already there.
    and if that field is in other model , how can i reach it.

【问题讨论】:

  • 这里的每个人都认为是字典的 vals 的类型是什么

标签: openerp odoo-9 odoo-10


【解决方案1】:

试试这个:

def method(self):
    if vals.get("field_name", False): 
        # Code if is not empty
    else:  
        # Code if is empty

【讨论】:

    【解决方案2】:

    您可以尝试以下代码,其中首先检查键是否在 vals 中。如果键可用,则检查值是否设置。

    def method(self, vals):
        if vals.has_key('field') and not vals.get('field',False):
            print "if logic"
        else:
            print "else logic"
    

    如果字段不为空且字段在其他模型中,那么您应该尝试使用 active_model

    您可以在调用方法的上下文中传递active_model,基于此您可以浏览记录或进行其他操作

    例如:

    def method(self, vals):
        if vals.has_key('field') and not vals.get('field',False):
            print "if logic"
        else:
            model=self._context.get('active_model')
            self.env[model].browse(model)
            print "else logic"
    
    self.with_context({'active_model':'model'}).method(vals)
    

    使用 with_context 用户可以传递 context 并根据上下文值轻松获取活动模型。

    这可能会对你有所帮助。

    【讨论】:

      【解决方案3】:
      def method(self, vals):
          if not vals.get('field'):
              #if empty use your logic
          else:
              #if not empty use data that was already there.
              #to print field value:
              print vals['field']
      
                  #and if that field is in other model , how can i reach it.
              you may reach it by search that field in other model.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多