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

what Difference between compute and onchange?

By
Demirel yakito
on 1/5/15, 1:51 AM 1,920 views

can anyone differentiate and usage of both compute and onchange...?

1
Mohit Kotak (MK)
On 1/5/15, 4:03 AM

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.

 

 

1
Pornima Mane
On 1/5/15, 3:45 AM

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.

0

Baiju

--Baiju--
3154
| 5 4 8
Kochi, India
--Baiju--
ODOO technical and functional

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju
On 1/5/15, 3:50 AM

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.

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: 1/5/15, 1:51 AM
Seen: 1920 times
Last updated: 3/16/15, 8:10 AM