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)