Community mailing list archives

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

Alexis de Lattre
- 12/14/2014 05:46:30
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