问题
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
when i use search_count
below:
loan_count = self.env['hr.loan'].search_count([('employee_id', '=', values['employee_id']), ('state', '=', 'approve'),
('balance_amount', '!=', 0)])
it always get count value even balance_amount
equals to Zero
回答1:
There are some things you have to do here.
- 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
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.
You either have to store the value or have to define a
search
method. Second approach is more difficult (in my experience).
来源:https://stackoverflow.com/questions/50035832/how-to-get-computed-field-value-in-search-orm-in-odoo