Skip to Content
Menu
This question has been flagged
1 Reply
9687 Views

I've tried every thing I could find on this forum, but I just don't get how to use the commands bellow. I guess it's simple once you figure it out.

My problem is I need to create records in a one2many relation, on the many side when I work on a record on the one side. Normally I just used rec.child_ids.create({'parent_id': rec.id}), but when working on that record the rec.id is not defined (odoo.Models.NewId...). Now I need help to use this commands to create the record, with the correct parent id. And a quick explanation for dummies would be great! :)

Fictional models, but that's the relation and behavior I need.

class ParentModel(models.Model):

    _name = 'parent.model'

    name = fields.Char()

    child_ids = fields.One2Many('child.model', 'parent_id')

    @api.depends('name')

    def create_method(self):

        for rec in self:

            rec.child_ids.create({'parent_name': rec.name, 'parent_id': rec.id})

class ChildModel(models.Model):

    _name = 'child.model'

    parent_name = fields.Char()

    parent_id = fields.Many2One('parent.model')



(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)
Avatar
Discard
Author Best Answer

OK... if no one knows... I'll answer it part myself. 

I found a lot off posts that you cannot create one2many in api.onchange, so I guess that's the same on api.depends. Did a work around so that I create records when I create the parent and in the api.depends will just change the values. (At least I hope it'll work this way else I'm fed).

To create the child elements wit those commands I need to use it on the parent and not on the child as I was trying.

def create_method(self):
    child_list = list()
    for data in config_datas:

        child_list.append((0, 0, {'parent_name': self.name, 'data': data.something}))  # don't need to add parent_id
    self.create({'name': self.name, 'child_ids': child_list})    


  EDIT:

Nope it doesn't update the child values! :( I had to create a button that triggers update the child records for all parents. How  can I use the buttons method in the form views Save button? Will probably ask this in a new question if I don't find the answer.

Avatar
Discard
Related Posts Replies Views Activity
3
May 17
10182
1
Sep 22
1010
2
Apr 20
3072
4
May 18
9450
1
Nov 17
2961