can anyone differentiate and usage of both compute and onchange...?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
Hello Randy,
On Change
The on_change attribute defines a method that is called when the content of a view field has changed.
This method takes at least arguments: cr, uid, ids, which are the three classical arguments and also the context dictionary. You can add parameters to the method. They must correspond to other fields defined in the view, and must also be defined in the XML with fields defined this way:
<field name="name_of_field" on_change="name_of_method(other_field'_1_', ..., other_field'_n_')"/>
The example below is from the sale order view.
You can use the ‘context’ keyword to access data in the context that can be used as params of the function.:
<field name="shop_id" on_change="onchange_shop_id(shop_id)"/>
def onchange_shop_id(self, cr, uid, ids, shop_id): v={} if shop_id: shop=self.pool.get('sale.shop').browse(cr,uid,shop_id) v['project_id']=shop.project_id.id if shop.pricelist_id.id: v['pricelist_id']=shop.pricelist_id.id v['payment_default_id']=shop.payment_default_id.id return {'value':v}
When editing the shop_id form field, the onchange_shop_id method of the sale_order object is called and returns a dictionary where the ‘value’ key contains a dictionary of the new value to use in the ‘project_id’, ‘pricelist_id’ and ‘payment_default_id’ fields.
Note that it is possible to change more than just the values of fields. For example, it is possible to change the value of some fields and the domain of other fields by returning a value of the form: return {‘domain’: d, ‘value’: value}
returns:
a dictionary with any mix of the following keys:
domain
A mapping of {field: domain}.
The returned domains should be set on the fields instead of the default ones.
value
A mapping of {field: value}}, the values will be set on the corresponding fields and may trigger new onchanges or attrs changes
warning A dict with the keys title and message. Both
are mandatory. Indicate that an error message should be displayed to the user.
Compute
Computed fields Regular fields with the name of the compute method:
classclass res_partner((Model):): ......
I hope it is useful to understand you.
hello Randy,
normally compute function is used for the fuction field.
which gets called when we save the record and onchange fuction
gets called when the value of field gets changed.
Compute field:
Fields can be computed (instead of read straight from the database) using the compute parameter. It must assign the computed value to the field
refer to https://www.odoo.com/documentation/8.0/reference/orm.html#computed-fields
onchange: updating UI on the fly
When a user changes a field’s value in a form (but hasn’t saved the form yet), it can be useful to automatically update other fields based on that value e.g. updating a final total when the tax is changed or a new invoice line is added.
refer to https://www.odoo.com/documentation/8.0/reference/orm.html#onchange-updating-ui-on-the-fly
-
computed fields are automatically checked and recomputed, they do not need an onchange
-
for non-computed fields, the onchange() decorator is used to provide new field values:
@api.onchange('field1', 'field2') # if these fields are changed, call method def check_change(self): if self.field1 < self.field2: self.field3 = True
the changes performed during the method are then sent to the client program and become visible to the user
-
Both computed fields and new-API onchanges are automatically called by the client without having to add them in views
-
It is possible to suppress the trigger from a specific field by adding on_change="0" in a view:
<field name="name" on_change="0"/>
will not trigger any interface update when the field is edited by the user, even if there are function fields or explicit onchange depending on that field.
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up