如何在odoo中搜索orm中获取计算字段值

时间:2018-04-26 06:03:25

标签: odoo odoo-10 odoo-9 odoo-11

我已在computed field中使用计算方法定义odoo 10,现在我想在search orm中获取其值,但其值仍为False,当我尝试store=True时它的价值没有改变。 如果有人有解决方案请告诉我,我会非常感激。 我的代码是:

balance_amount = fields.Float(string="Balance Amount", compute='_compute_loan_amount')

@api.one
def _compute_loan_amount(self):
    total_paid = 0.0
    for loan in self:
        for line in loan.loan_lines:
            if line.paid:
                total_paid += line.amount
        balance_amount = loan.loan_amount - total_paid
        self.total_amount = loan.loan_amount
        self.balance_amount = balance_amount
        self.total_paid_amount = total_paid

当我使用下面的search_count时:

loan_count = self.env['hr.loan'].search_count([('employee_id', '=', values['employee_id']), ('state', '=', 'approve'),
                                                   ('balance_amount', '!=', 0)])

即使balance_amount等于Zero

,也始终会获得计数值

1 个答案:

答案 0 :(得分:3)

你有一些事情要做。

  1. 定义重新计算依赖项,并使用@api.multi和for self上的for循环,或使用api.one并跳过for循环。如果我理解您的计算方法正确:
  2. @api.multi
    @api.depends('loan_lines.paid', 'loan_lines.amount', 'loan_amount')
    def _compute_loan_amount(self):
        for loan in self:
            total_paid = 0.0
            for line in loan.loan_lines:
                if line.paid:
                    total_paid += line.amount
            balance_amount = loan.loan_amount - total_paid
            loan.total_amount = loan.loan_amount  # ???
            loan.balance_amount = balance_amount
            loan.total_paid_amount = total_paid
    
    1. 尝试使用例如浮点数2个小数,因为浮点数可以产生0.000000000000000002之类的值,这会破坏你的搜索。

    2. 您必须存储值或必须定义search方法。第二种方法更难(根据我的经验)。