Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2144 Lượt xem

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?


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

can you paste some of your code ?

Tác giả

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.

Tác giả

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

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