Community mailing list archives

community@mail.odoo.com

Re: V8 New API questions

by
Camptocamp SA, Guewen Baconnier - Software Developer, Camptocamp
- 11/25/2014 08:13:46
Hi,

On 11/25/2014 01:43 PM, Jos De Graeve wrote:
> Hi,
> 
> I have two questions about the new on-change api:
> 
> How would one implement functionality like the "warning" module with the
> new API: https://github.com/odoo/odoo/blob/8.0/addons/warning/warning.py#L56
> Is it required to override the 'onchange' method
> https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L5700 or is
> there another way to do this?

You can still returns a warning with the new api:

    @api.onchange('partner_id')
    def onchange_partner_id(self):
        return {'warning': {'title': 'Warning', 'message': 'Message'}}
> 
> The env.context dictionary is a frozen dict, is there still a way to
> insert the context with a variable ?  I wanted to use the on-change api
> to set a new default value in the context.
The new way to change the context is:

    records self.with_context(new_key='abc').browse(ids)  # current
context extended with {'new_key': 'abc'} in records

    records = self.with_context({'new_key': 'abc'}).browse(ids)  #
current context replaced by {'new_key': 'abc'} in records


> My use case is sale order line entry: when a user creates a new order
> line and changes a certain field, a new default value is set in the
> context, so that the next created order line would have the same value
> as entered in the previous order line.
I don't think it is possible with the context. IMO relying on side
effects by modifying the context "in place" had always been a dangerous
and difficult to track operation so I have been glad when I saw that a
frozen dict was used in the new api (although the case you describe here
is not so bad, it looks more like a "session" object). Now, I don't know
if there is another way to do what you want to do.

> 
> Tnx,
> 
> Jos De Graeve - Apertoso business ICT
> 
> http://www.apertoso.be/
> 
> Guido Gezellelaan 16 - B-9800 Deinze - Belgium
>  
> Direct: +32 9 381 64 51
> General:+32 9 381 64 50
> Mobile: +32 475 54 68 80
> mail/im/skype: Jos.DeGraeve@apertoso.be
> <mailto:Jos.DeGraeve@apertoso.be> - apertoso
> 


-- 
Guewen Baconnier
Business Solutions Software Developer

Camptocamp SA
PSE A, CH-1015 Lausanne
Phone: +41 21 619 10 39
Office: +41 21 619 10 10
http://www.camptocamp.com/