Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recompute stored functional field values in Odoo?

Tags:

openerp

odoo-8

Sometimes stored fields must be recomputed, but triggers can not be launched (e.g. in case of SQL injection).

How to recompute them an easy way?

like image 689
and3p Avatar asked Dec 04 '15 13:12

and3p


3 Answers

In v13 the above syntax still works, but the add_todo should be replaced by add_to_compute:

model = env['account.move']
env.add_to_compute(model._fields['amount_total'], model.search([]))
model.recompute()

To avoid loading all objects into memory use ids instead:

model = env['account.move']
ids = [x.get('id') for x in model.search_read(domain, ['id'])]
env.all.tocompute[model._fields['amount_total']].update(ids)
model.recompute()
like image 157
Robrecht Avatar answered Sep 30 '22 13:09

Robrecht


(Because I came here via google:)

You can also do this from the Odoo Shell:

# python odoo.py shell -c openerp-server.conf  -d <database>

>>> model = env['account.invoice']
>>> env.add_todo(model._fields['amount_total'], model.search([]))
>>> model.recompute()
>>> env.cr.commit()

Odoo shell is available in 9, 10 and via an OCA module in 8.

like image 35
RoelAdriaans Avatar answered Sep 30 '22 14:09

RoelAdriaans


In v8.0 (should work in 9.0 too) you can do it like that:

# Recompute amount_total for account.invoice

env.add_todo(model._fields['amount_total'], object)
model.recompute()

# where
# object - recordset of instances to recompute field for
# model - recordset instances model

Above code can be used in server action directly.

like image 23
and3p Avatar answered Sep 30 '22 12:09

and3p