Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

[8.0] [API] Computed Fields - Using the Search Function

By
John
on 6/5/15, 10:58 AM 844 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?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/5/15, 10:58 AM
Seen: 844 times
Last updated: 6/8/15, 9:59 AM