How to get computed field value in search orm in odoo

[亡魂溺海] 提交于 2020-01-06 05:42:46

问题


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_countbelow:

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.

  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. 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.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!