Community mailing list archives


Re: Question on new API: @api.onchange and create()

Alexis de Lattre
- 12/14/2014 06:18:46

Thanks for your answer. Do you mean that I should:
1) call create:
self.env['donation.line'].create({'product_id': 42})
2) then execute product_id_change, that will trigger a write on the donation line ?


Le 14/12/2014 11:57, Pedro Manuel Baeza Romero a écrit :
<blockquote cite="" type="cite">
Hi, Alexis,

You can make the same way inheriting onchange method and calling super. The only problem is again that you need at least one first module to depend on that declares the initial onchange, but when you have it (in this case, the one that defines donation.line object), you can have two separate modules that both overrides onchange method, call super, and you will get that both are executed. In this new type of onchange, you don't return the values, you write directly with ActiveRecord pattern, so you can write any field you want on each method.


2014-12-14 11:48 GMT+01:00 Alexis de Lattre <>:
I am currently porting the Philemon project 
( to the new API, and I have a question:

I have a "donation" object, with a O2M field to a "donation.line" 
object. This donation.line object has a "product_id" field with an 
on_change that sets some values of the donation.line from the values of 

With the old API, a classical scenario when creating a donation.line via 
python code (called via a button for example), would be:
1) select the product (via XMLID for example)
2) then call the product_id_change() method:

res = self.pool['donation.line'].product_id_change(cr,  uid,  [],  stay_donation_product_id,  context=context)

3) then use res['value'] as a base to generate the "vals" dict that will 
be passed to the create(cr, uid, vals, context=context) method

Example of such code is here, line 70:

The advantage of this method is that, if I have a module B that adds a 
field on donation.line and inherit the product_id_change() method, then 
my module A will get the default value for that field, even if my module 
A doesn't depend on the module B. That's what happens in the module 
donation_stay and the module donation_tax_receipt (they don't depend on 
each other).

I don't know how to have a similar thing with the new API: my 
product_id_change() method is now defined with:

def _product_id_change(self):
     if self.product_id and self.product_id.list_price:
         self.unit_price = self.product_id.list_price

I don't see a way to call the product_id_change() method and take 
advantage of its result for the create() method.

For the moment, the only solution I see is to keep in the XML view:

and have :

def product_id_change(self, product_id):
     res = {'value': {}}
     return res

But it seems to me as a "return in the past" i.e. something we should 
avoid with the new API (but I see that in odoo v8, in 
addons/account/, it's still made that way)

What do you think ? Any idea ?

Alexis de Lattre

Post to:

Post to: