Odoo : Acces a column from many2one

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 13:14:31

I finally solved my problem :

When you create an onchange, it seams that it will only change the javascript, if the field you try to modify, thanks to an onchange on an other field, isn't in your tree, it won't be changed. What I figured out, doing some attempt while looking at phppgadmin:

  • When you change your field, it update the javascript, nothing is done in the database.
  • Once you saved your change, the javascript seams to analyse the fields and update the database with those.

So when something is not in the xml, you can't use onchange on it. It's a real pain if you want to change something in another model or module. But more of that it was really stupid to try this and it perfectly make sense: the purpose of onchange is to update before any registration is done, the only interest is to show it while your still modify it.

The solution was quite simple thanks to the Odoo 8.0 API. Here is an exemple (This is in res partner):

'my_field': field.many2one(compute='_compute_second_field', store=True, readonly=True)

@api.depends('name_ids.type') #name_ids is the one2many reverse of name's many2one in model1
for record in self:
    if self.name_ids: #if some names are pointing to this, name_ids is not empty
        record.model3_id = 2
    else: #if doesn't exist, default value 
        record.model3_id = 0

So when you create in res_partner, this res_partner is still not in model1, take value '0'. Then you add it in model1, take value 2. If you remove him from model1, take value '0' again (removing is still a modification).

for openerp-7 it is 'fields.function' (with type='many2one' as argument and store={('model1':_other_function, ['type'], 10)} but I didn't managed to finish it this way, so I won't show it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!