1
votes

I am trying to write values in one2many field. I am using Odoo v8. I have a wizard with one2many field. A button is associated and on clicking the button I am using write method to add values into one2many field.My button function is being called but it closes the wizard without doing anything. I dont get any error or exception.

My XML code is as:

<button name="split_qty" type="object" string="Split" class="oe_highlight"/>

My python code is as:

for i in range(1,11):
            self.write({'item_ids':[(0, 0, {'product_id':pID,'quantity ':1, 'sourceloc_id':sID,'destinationloc_id':dID})]}) 

Any help or guidance on this will be helpful. Thanks

1

1 Answers

0
votes

You mentioned that you are using a wizard, and using self.write it means you are changing the wizard data not the main model. I guess this is the problem. you should use self.env[target model].write instead.

Also,it is not a good practice to call the write method in a loop, instead use this:

recs=[]
for i in range(1,11):
    recs.append((0, 0,{'product_id':pID,'quantity':1,'sourceloc_id':sID,'destinationloc_id':dID})]}) 
self.env['your.model'].write({'item_ids':recs)

or shorter more python like version:

self.env['your.model'].write({'item_ids':[(0, 0,{'product_id':pID,'quantity':1,'sourceloc_id':sID,'destinationloc_id':dID}) for i in range(1,11)])

p.s. you haven't used i, is it intentional? If so, the code can be shorter and better:

self.env['your.model'].write({'item_ids':[(0, 0,{'product_id':pID,'quantity':1,'sourceloc_id':sID,'destinationloc_id':dID})]*11)