2
votes

I Have two Models in my odoo moduel Model_2 have a One2many field point on Model_1 I want to create un function in Model_2, this function searche some objects from model_1 then create a object from Model_2, and add le search result to this object

class Model_2(models.Model):
    _name = 'mymodule.model_2'
    _description = 'Model_2

    model_2_ids = fields.One2many('mymodule.model_1', 'model_1_id')

    def my_function(self):
        model_1_objects = self.env['mymodule.model_1'].search()
        self.create({'model_2_ids': model_1_objects})

I Have an error in the last line of my code I want to add model_1_objects to the created object, How can I do this ?

1

1 Answers

2
votes

I believe you can add the model_1_objects like this:

self.create({
    'model_2_ids': [(6, 0, model_1_objects.ids)] 
})

You can learn more on the CRUD section in Model Reference here: https://www.odoo.com/documentation/10.0/reference/orm.html#model-reference