One2many field on_change function can't change

2019-04-02 08:37发布

I have these two fields.

'name'              : fields.char('Name'),
'addresses'         : fields.one2many('res.partner.address', 'partner','Addresses'),

This function:

def addresses_change(self, cr, uid, ids, name, addresses, context=None):
        value = {}

        new_addresses = []
        address_pool = self.pool.get('res.partner.address')
        for address in address_pool.browse(cr, uid, addresses[0][2], context=context):
            new_addresses.append((1,address.id,{'street':'wall street','zip':'7777','partner': ids[0],'active':True}))

        value.update(name='whatever')
        value.update(addresses=new_addresses)
        return {'value':value}

And these view fields:

<field name="name" on_change="addresses_change(name,addresses)"/>
<field name="addresses" on_change="addresses_change(name,addresses)"/>

Now when I change name, both name and addresses are updated. But when I change addresses its own value isn't updated but the name is updated. So this bizarre behavior affects only one2many fields. Why is this?

And how do I add on_change event to one2many fields that can update its own value?

EDIT: I found out that this is might be a limitation in odoo, have they fixed this issue? Link to the issue

2条回答
迷人小祖宗
2楼-- · 2019-04-02 09:14

Apply the following patch on the lastest version of models.py (the version commited on Tue Aug 4 15:22:33 2015 +0200):

--- a/openerp/models.py
+++ b/openerp/models.py
@@ -5897,9 +5897,9 @@ class BaseModel(object):

         # At the moment, the client does not support updates on a *2many field
         # while this one is modified by the user.
-        if field_name and not isinstance(field_name, list) and \
-                self._fields[field_name].type in ('one2many', 'many2many'):
-            result['value'].pop(field_name, None)
+        ## if field_name and not isinstance(field_name, list) and \
+        ##         self._fields[field_name].type in ('one2many', 'many2many'):
+        ##     result['value'].pop(field_name, None)

         return result

In other words, just comment the lines 5900 to 5902 of the openerp/models.py file.

Of course, there is a big disadvantage with this solution - you need to apply the patch every time the models.py file is updated in the Odoo distribution you use.

There is a considerable risk too - they say that the Web client is not dealing well with one2many and many2many fields updated in onchange event. I did not discover any problem with that right now but I'll continue to test my development installation of Odoo...

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-02 09:27

I created a Pull Request to odoo version 8

https://github.com/odoo/odoo/issues/2693

with the changes mentioned in the issues and here

    if field_name and not isinstance(field_name, list) and \
-                self._fields[field_name].type in ('one2many', 'many2many'):
+        if field_name and not isinstance(field_name, list):
查看更多
登录 后发表回答