Odoo Help

Welcome!

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.

1

[odoo 8 ] : How to update value of one2many field with at second level by using create and write method using API?

By
Prince Caspion
on 7/11/16, 2:02 AM 1,157 views

Hello All,

I've asked this Question again, because i was not able to EDIT my question.

I'm having problem on overriding create method.

I've one2many field in my current model, I want to update the value of that field when new record created, and this one2many field's co-model has it's own one2many fields.

So, basically there is two levels of one2many fields.

My one2many field has one2many field, I want to create record for that model as well,

How can i achieve this?

Any help on this appreciated.

This is need to be done using api coding standard.

EDIT

E.g.

Class A(models.Model):    
_name = 'main.model'
x_ids = fields.one2Many('sub.model','main_id',"Test")

   

Class B(models.Model)    
_name = 'sub.model'   
main_id = fields.Many2one('main.model', "Main Model")
#remaining fields.
xx_ids = fields.One2many('another.sub.model','sub_model_id',"Test")
Class C(models.Model)    
_name = 'another.submodel'
sub_model_id = fields.Many2one('sub.model',"Sub Model")
#remaining fields..go here   

Consider this is model structure, now when i create record of Class A model, I want to updated one2many field of Class B and Class B is having Class C one2many field, so how to update the values or create records with 2 level of one2many field? is there any option?

I am looking for the solution for both side create and write method. Please anyone has idea than it will be really helpful.

Regards,

Prince

1
kirubanidhi
On 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',

})]

})

1

deep

--deep--
1617
| 5 4 7
Bengaluru, India
--deep--

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

deep
On 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)

0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 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 ...

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

Prince Caspion
on 7/11/16, 12:28 PM

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

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

Hi, can you share your code some where ?

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

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

1 follower(s)

Stats

Asked: 7/11/16, 2:02 AM
Seen: 1157 times
Last updated: 7/15/16, 5:20 AM