Skip to Content
Menu
This question has been flagged

I would like to understand when this field changes. It is part of sale.py model.

I have added a field "global discount" to sale.order model and  I would like amount_untaxed to change when field "global discount" changes, what should I do?


'amount_untaxed': fields.function(_amount_all_wrapper, 
  digits_compute=dp.get_precision('Account'), 
  string='Untaxed Amount',
              store={
                    'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
     'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
              },
  multi='sums', 
  help="The amount without tax.", 
  track_visibility='always'),


Thanks

Avatar
Discard
Best Answer

Hello,

The store keyword used to enhance the performance; since the functional field will be computed each time it'll be visible to the user.

without the store keyword [store=False], the functional field will not be stored in the database [e.g you can just access it by a browse record].

If you use store=True [the default is False]; your functional field will be stored in the DB and will be calculated for just one time.

- To tell the server when to recompute this filed you'll use a dictionary as

store={'model_name': (trigger_ids, [trigger_fields], priority), ...}

as in your example:

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

the means: the field amount_untaxed will be recomputed whenever there is a changes in the 'order_line' field of the sale.order and

'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),

To recompute the field whenever there is a changes in the fields: 'price_unit', 'tax_id', 'discount', 'product_uom_qty' of the sale.order.line ...

the _get_order function: will return the [id] of parent sale order of the sale.order.line


For your question, I think you need to update the line as:

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

I hope this could helps ...
Avatar
Discard
Author

It really helps, I managed to discover that adding global_discount triggered the field update, but I did not manage to fully understand how it fully worked. Your explanation is comprehensive and clear, thanks a lot.

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

Does this mean, if order_line field of any sale_order is changed, then this functional field will get triggered;

'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),

And in above functional field, if mentioned fields of sale_order id returned by _get_order is changed , then field will get triggered.

Sorry for a stupid doubt; but how does the field knows that , record of sale_order class is modified?

Related Posts Replies Views Activity
1
Aug 24
2949
1
Feb 20
1560
3
Mar 18
4750
1
Aug 17
2970
3
Apr 17
6794