如何在odoo中的搜索orm中获取计算的字段值 [英] How to get computed field value in search orm in odoo

查看:92
本文介绍了如何在odoo中的搜索orm中获取计算的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在odoo 10中用计算方法定义了computed field,现在我想在search orm中获取其值,但其值仍为False,当我尝试store=True时,其值未更改. 如果有人有解决方案,请让我知道,我将非常感谢. 我的代码是:

I have defined a computed field with compute method in odoo 10 and now i want to get its value in search orm but its value remain False, and when I tried store=True its value not being changed. if anyone has solution please let me know, I'll highly thankful. My code is:

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

it always get count value even balance_amount equals to Zero

推荐答案

在这里您需要做一些事情.

There are some things you have to do here.

  1. 定义重新计算依赖性,并使用@api.multiself上的for循环,或使用api.one并跳过for循环.如果我了解您的计算方法正确:
  1. Define recomputation dependencies and either use @api.multi and a for loop over self or use api.one and skip a for loop. If i understand your compute methode correct:

@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这样的值,这会破坏搜索.

  1. Try to use a float rounding with e.g. 2 decimals, because floats can produce values like 0.000000000000000002 and that would break your search.

您要么必须存储值,要么必须定义search方法.第二种方法更困难(以我的经验).

You either have to store the value or have to define a search method. Second approach is more difficult (in my experience).

这篇关于如何在odoo中的搜索orm中获取计算的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆