How to change values of a one2many field in an on_change event?

Asked by Christophe Hanon (www.adins.be)

I am trying to write an on_change handler for a one2many field.

basically, what I want to do is checking when a line is added to sale.order and in some case add an extra line.

I catch the event (this is is my simplest handler for the time being:

 def product_order_line_change(self, cr, uid, ids, order_line,partner_id,pricelist_id,context={}):
             return {'value': {'order_line': order_line }}

order_line is a list of list...

So I am just sending back the value I received (not very useful, but just for test...).

On the gtk client side, the dialog does not close, in the console of the gtk

I see : AttributeError: 'list' object has no attribute 'keys'

So what am I supposed to send back for a one2many field ?

Question information

Revision history for this message
Christophe Hanon (www.adins.be) (chanon) said :
#1

This is the answer from support I got

You can not achieve your desired goal by on_change method, because one2many field is saved only after saving parent record as per the current architecture. So you will not get the id of Sale Order Line.

We suggest you to override the create method of Sale Order Line object for achieving your goal.

For example,
    def create(self, cr, uid, vals, context=None):
        # check vals
        # put conditions
        # put values in vals dict according to your need
        return super(sale_order_line, self).create(cr, uid, vals, context=context)
------------------------

Just in case somebody look at it, I found that you can change the content of the one2many just by sending an array of ids []. Yet support is right because it is not possible to link to new non existing lines (in my cae sale.order.line ) in DB.

Revision history for this message
Christophe Hanon (www.adins.be) (chanon) said :
#2

Created a bug / enhancement request.

Revision history for this message
Lorenzo Battistini (elbati) said :
#3

See the on_change methods of 'account.voucher' form