Odoo Help


[V8] Need help to understand old api computed field

Aurélien Rivet
on 7/22/16, 10:24 AM 617 views

Hi ! Here is an old api code from the sale_margin module:

class sale_order(osv.osv):
    _inherit = "sale.order"
    def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        for sale in self.browse(cr, uid, ids, context=context):
            result[sale.id] = 0.0
            for line in sale.order_line:
                if line.state == 'cancel':
                result[sale.id] += line.margin or 0.0
        return result
    def _get_order(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
            result[line.order_id.id] = True
        return result.keys()
    _columns = {
        'margin': fields.function(_product_margin, string='Margin', help="It gives profitability by calculating the difference between the Unit Price and the cost price.", store={
                'sale.order.line': (_get_order, ['margin', 'purchase_price', 'order_id'], 20),
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 20),
                }, digits_compute= dp.get_precision('Product Price')),

I just can't understand what the code in bold means. Could someone explain what it does imply ?

Is it just storing the computed field inside the db ? And why a store=True kwarg wouldn't be sufficient ?

For various reasons, I need to override the margin computed field. How can I do it since it seems to be quite special case?

--Ahmed M.Elmubarak--

Ahmed M.Elmubarak
On 7/23/16, 3:12 AM


The bold lines determines when the computes field will be recalculated, it is some how equivalent to @api.depends in the new API.   

'sale.order.line': (_get_order, ['margin', 'purchase_price', 'order_id'], 20),

Means: if the ids got by _get_order of 'sale.order.line' recorded changes in : 'margin', 'purchase_price', and 'order_id' Then recompute the field.

'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 20)

Means: if this sale order changed the 'order_line' then recompute this field.

- and It uses like this to enhance the performance ... [if the store = false: it will always be recomputed when it visible, and if the store = True: it will computed just for the first time].

Hope this could helps a little bit ...

Hi thanks for your answer, I understand a little bit more right now. But my problem is the fact that ovewriting the field in an other module produces a strange bug ('undefined get method'). I don't know how to do it properly.

Aurélien Rivet
on 7/25/16, 8:21 AM

can you paste some of your code ?

Ahmed M.Elmubarak
on 7/26/16, 2:27 AM

Dear Ahmed many thanks for your help.

Thanks to this issue on Github https://github.com/odoo/odoo/issues/5112, I could understand that what I wanted to achieve is properly impossible. I did it in another way, reimplementing my own margin.

Aurélien Rivet
on 7/26/16, 9:29 AM

mistake in the issue URL : https://github.com/odoo/odoo/issues/3922

Aurélien Rivet
on 7/26/16, 9:32 AM

Dear Rivet, happy that you managed to resolve it :)

Ahmed M.Elmubarak
on 7/26/16, 9:39 AM

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/22/16, 10:24 AM
Seen: 617 times
Last updated: 7/26/16, 9:30 AM