Jihane Hemicha
Jihane Hemicha

Reputation: 165

concept of create method odoo8

I'm new at using odoo, so i faced an override of create method but still don't quite understand the concept. This is the create method I used :

@api.model
def create(self, vals):
    vals['task_idd'] = self.env['crane.task'].create({'equipment_id': vals['equipment_id'], 'type': vals['type']}).id
    vals['task_ids'] = [(4, vals['task_idd'])]
    return super(crane_workorder, self).create(vals)

this code works perfectly i had a one2many relation that has been converted into one2one(logically), so i override the create method till i can create two records from two different classes... PS1: equipment_id & type are required fields, but i don't understand how does it work ...

Upvotes: 0

Views: 162

Answers (1)

Phillip Stack
Phillip Stack

Reputation: 3378

If what you mean is how does the list containing a single tuple [(4, vals['task_idd'])] give the desired result here is my understanding.

When Odoo is either creating or writing to a record which is a relation field Odoo uses a command syntax which is the structure you used in your code. Basically when writing or creating a related field from the perspective of any particular record Odoo tries to take away the ambiguity by creating a specific series of commands to use in these circumstances.

For instance if you need to write to a One2many field how is Odoo supposed to know if your are adding a new record to the list of many other records or are you saying that the list of related records is this new record you have referenced in your code.

So imagine you have a tree with a One2many relation with its Apples. If you were writing to a database recording another apple for the tree are you saying that this apple is to be added to the list of existing apples or is it replacing the existing list and should be considered the only apple that belongs to the tree.

So Odoo uses this tuple structure and has a process for parsing the tuple and determining how to handle its values and what to write to the database.

Here is a screen shot of the documentation Odoo provides on the matter. Here is the link to the page

Odoo Documentation on Relation Fields

Upvotes: 1

Related Questions