Odoo Help

2
6 Answers
3
Avatar
--deep--

Techno-Functional Associate with 7+ years of experience in Odoo (formerly known as OpenERP).

deep
7/15/16, 5:20 AM

You can achieve that in 2 ways:

Case 1: Default Values

if you have fixed data, then you can set default values in both objects i.e in ClassA set default values for 'x_ids' and in ClassB set default values for 'xx_ids'

Case 2: Create/Write method

In the First object (ClassA): while creating itself you can add its lines using One2Many and/or Many2Many semantics.


According to your sample object hierarchy:


@api.model

def create(self, vals):

valsC = {....} # dict of values needed for creating ClassC

valsB = {
'xx_ids': [(0, 0, valsC)],

          ....} # dict of values needed for creating ClassB

vals['x_ids'] = [(0, 0, valsB)] # appending it into the values of ClassA

return super(A, self).create(vals)



In Odoo, at the time of creating a record, you can pass values to its child be it a One2Many or Many2Many by using its semantics, here [(0,0,{})] represents am trying to create new One2Many record.

Note:

In valsB, I have appended the values of ClassC, and this dict am appending the original Vals of Class A, which will be like passing nested dict of values into ClassA

Similarly there exists a semantics for editing/updating, deleting and so, please refer the below samples which are used for both One2Many & Many2Many.


(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

1
kirubanidhi
7/11/16, 5:40 AM

Update  A SAMPLE one2many record in odoo9, 

search_var = self.browse(self.id)

search_var.update({

'stud_ids': [(0,_, {

'reg_no':4200,

'dob': '04/01/1992',

'stud_name':'amuthan',

})]

})

0
Avatar
--Qutechs, Ahmed M.Elmubarak--


Qutechs, Ahmed M.Elmubarak
7/11/16, 3:18 AM

Hello,

In creation you need to override the create method of class A to be something as:

@api.model

def create(self, vals):

res = super(A, self).create(vals)

#creating C record

c_res = self.env['another.submodel'].create({'field_name': value, ...})

#creating B record

b_res = self.env['sub.model'].create({'field_name': value, ...})

#Adding C to B

b_res.xx_ids += c_res

#Adding B to A [it is also a One2many]

res.x_ids += b_res

return res

You can do similar thing in the write method with some modifications ...


Hope this could helps ...

3 Comments
Prince Caspion
7/11/16, 12:28 PM

@Ahmed: Thanks for the nice example, i will try and check it.

Prince Caspion
7/12/16, 4:22 AM

Ahmed given solution does not work , gives me this error, defaults.update(values)

ValueError: dictionary update sequence element #0 has length 3; 2 is required

Qutechs, Qutechs, Ahmed M.Elmubarak
7/12/16, 5:19 AM

Hi, can you share your code some where ?

Ask a Question
Writer
Keep Informed
2 follower(s)
About This Community

This platform 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.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now