我已在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
答案 0 :(得分:3)
你有一些事情要做。
@api.multi
和for self
上的for循环,或使用api.one
并跳过for循环。如果我理解您的计算方法正确:@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之类的值,这会破坏你的搜索。
您必须存储值或必须定义search
方法。第二种方法更难(根据我的经验)。