其他模块中的 Odoo 检查字段

Odoo check field in other module

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

我希望能把我的观点说清楚。

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.

试试这个:

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

您可以尝试以下代码,其中首先检查密钥是否在 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 & 基于上下文值,您可以轻松地动态获取活动模型。

这可能对您有所帮助。

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.