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

[V8] Need help to understand old api computed field

By
Aurélien Rivet
on 7/22/16, 10:24 AM 215 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':
                    continue
                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?


0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


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

Hello,

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

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: 7/22/16, 10:24 AM
Seen: 215 times
Last updated: 7/26/16, 9:30 AM