Community mailing list archives

community@mail.odoo.com

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

by
Pedro M. Baeza
- 12/14/2014 05:54:17
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.

Regards.

2014-12-14 11:48 GMT+01:00 Alexis de Lattre <alexis@via.ecp.fr>:
I am currently porting the Philemon project 
(https://code.launchpad.net/philemon) 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 
product.product.

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:
http://bazaar.launchpad.net/~philemon-core-editors/philemon/trunk/view/head:/donation_stay/wizard/create_donation_stay.py

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:

@api.onchange('product_id')
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 :

@api.multi
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/account_invoice.py, it's still made that way)

What do you think ? Any idea ?

Alexis de Lattre

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe