Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Access one2many list in many2one onchange event

Harald F
on 11/2/15, 9:01 AM 1,884 views

i want to acccess a one2many field in the onchange event of the corresponding many2one field.

I have defined 2 Models:

class model_a(osv.osv_memory):
_name = "model.a"
_columns = {'b_ids' : fields.one2many('model.b', 'a_id')}

def test(self):
print "Onchange Function model.a"
for line in self.b_ids:
print line.field1

class model_b(osv.osv_memory):
_name = "model.b"
_columns = {'a_id' : fields.many2one('model.a'),
'field1' : fields.char('field1')}

@api.onchange('field1', 'a_id')
def test(self):
print "Onchange Function model.b"
for line in self.a_id.b_ids:
print self.a_id
print line.field1

There is a view where you can edit the List:

        <record id="model_a_tree_view" model="ir.ui.view">
<field name="name">model_a_tree_view</field>
<field name="model">model.a</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Test">
<group >
<group colspan="4">
<field name="b_ids" widget="one2many_list" colspan="4">
<tree string="b_s" editable="bottom" >
<field name="a_id" invisible="1" />
<field name="field1" />

I expected to see all model.b entries in the one2many field b_ids. Instead i get only the current entry.

I could use the onchange funtion of  model.a field b_ids, but i cant see wich line has changed and i would have to revalidate all entries.

Output 1st Element added (the field1 text is still blank):

Onchange Function model.a
Onchange Function model.b
model.a(<openerp.models.NewId object at 0xad20f3ec>,)

Output 1st Element added (field1 = Test 1):

Onchange Function model.b
model.a(<openerp.models.NewId object at 0xad21658c>,)
Test 1
Onchange Function model.a
Test 1

Everything works like excpected so far. If i add another element (field1 = Test 2) the onchange function in Model b cant see the first entry:

Onchange Function model.b
model.a(<openerp.models.NewId object at 0xad2298ac>,)
Test 2
Onchange Function model.a
Test 1
Test 2

In the onchange function for model.a self.b_ids is:

b: model.b(<openerp.models.NewId object at 0xad1c73ec>, <openerp.models.NewId object at 0xad1c7b6c>)

in the onchange function for model.b self.a_id.b_ids is:

b: model.b(<openerp.models.NewId object at 0xad1d3b6c>,)

Is it possible to see all entries in the onchange function for field1 or see wich item was modified in the onchange function for a one2many field (including new lines without id)?  

What version of Odoo are you using?

Axel Mendoza
on 11/2/15, 2:17 PM

I am using Verison 8.0 cloned today from GitHub Repository, created a new Database and wrote a Demo Module.

Harald F
on 11/3/15, 3:22 AM

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 11/3/15, 1:34 AM

Hello Harald,

In parameter of "api.onchange", you should pass those fields which you want to access in your onchange method.

It should be as follow:

@api.onchange('field1', 'a_id')
def onchange(self):
print self.a_id
print self.a_id.b_ids

Hope this helps.

I tried you suggestion and edited the original post. Still the same problem :(

Harald F
on 11/3/15, 3:54 AM

Make sure "a_id" has some value.

Serpent Consulting Services Pvt. Ltd.
on 11/3/15, 5:02 AM

it has a value, the value(see the edited post) is: model.a(,)

Harald F
on 11/3/15, 6:02 AM

Drees Far

--Drees Far--
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 11/3/15, 3:55 AM

Hi friend :)

 the onchange() decorator is used to provide new field values: as shown through this example:

@api.onchange('field1', 'field2') # if these fields are changed, call method

def check_change(self):

if self.field1 < self.field2:

self.field3 = True

May this help you.

Here are some useful links:




Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)


Asked: 11/2/15, 9:01 AM
Seen: 1884 times
Last updated: 8/8/16, 5:40 AM