This question has been flagged
5164 Views

 I implemented a computed field like below:

class AccountAnalyticAccount(models.Model):
_inherit = 'account.analytic.account'

balance2 = fields.Float(string="Balanced", compute='_balance', search='_search_balanced')
# debit = fields.Float(compute='_debit_credit_bal_qttyi')
# credit = fields.Float(compute='_debit_credit_bal_qttyi')
# quantity = fields.Float(compute='_debit_credit_bal_qttyi')
@api.one
@api.depends('balance')
def _search_balanced(self, operator, operand):
'''Search the functional field balance'''
# for some reason, this method never gets called!
pdb.set_trace()
self.env.cr.execute('''
SELECT a.id

FROM account_analytic_account a LEFT JOIN account_analytic_line l on (a.id=l.account_id)
WHERE COALESCE(SUM(l.amount),0)
''' + operator + ' ' + float(operand)
)
rows = self.env.cr.fetchall()
res = [('ids', 'in', rows)]
if not (rows):
res = 0.0
return res

@api.multi
@api.depends('balance')
def _balance(self):
for acct in self:
acct.balance2 = acct.balance
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


As you can see, I have a pdb.set_trace() in there and would expect that I would get to the breakpoint when I run a command with that field in the domain.  Instead, when I run the following from XML-RPC, I get all the rows that have parent_id.code == 'RET001', but I get all rows that have 0.0 in the balance without ever running into my trace:

res = self._model.execute_kw(self._db,self.uid, self._pwd,"account.analytic.account","search",[[('balance2', '!=', '0.0'),('parent_id.code','=','RET001')]],{}) 


How do I implement computed field search correctly?

Avatar
Discard