其他模块中的Odoo检查字段

时间:2017-04-12 17:33:25

标签: openerp odoo-9 odoo-10

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

我希望尽力说清楚。

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.

3 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

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

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 ,用户可以传递上下文&根据上下文值,您可以轻松地动态获取活动模型。

这可能会对你有帮助。

答案 2 :(得分:1)

DataTable Dt = (DataTable)dataGridView1.DataSource;

if (int.TryParse(Dt.Compute("SUM(Qty)", "").ToString(), out Sum))
{
    textBoxQtyinStock.Text = Sum.ToString("N0");
}