Search code examples
odooodoo-12

How Do I Add Many2One Data In Files


I have a few interdependent models with Many2One and One2Many relations in between them.

I have the views all set up but now that I'm adding data through data files to test everything I find that I have no idea how to go about adding these records.

Should I do a run where I basically declare the records while keeping relation values null then update the records with relations later?

Additionally, what's the value of a relational field supposed to be? An array or something?


Solution

  • You can always use the demo and data files of builtin modules as reference

    as you can see here category_ids is is Many2many field (same applies for one2many fields)

    <field name="category_ids" eval="[(6, 0, [ref('employee_category_2')])]"/> to understand the value of the eval attribute read the following:

    (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)
    

    and for the ref function in it's value ( i.e this part ref('employee_category_2') ) it's a python function evaluated when the data file is loaded, it takes the XML recordID of the related field ( sometimes you might need to use external ID - for example in this case the external id of the record is hr.employee_category_2)

    References: 1 2