如何在odoo中的搜索orm中获取计算的字段值 [英] How to get computed field value in search orm in odoo
问题描述
我在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.
- 定义重新计算依赖性,并使用
@api.multi
和self
上的for循环,或使用api.one
并跳过for循环.如果我了解您的计算方法正确:
- Define recomputation dependencies and either use
@api.multi
and a for loop overself
or useapi.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
-
尝试使用浮点舍入,例如小数点后2位,因为浮点数可以产生0.000000000000000002这样的值,这会破坏搜索.
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屋!