insert into many2many odoo (former openerp)

m3asmi picture m3asmi · Feb 21, 2012 · Viewed 48.5k times · Source

I'm trying to insert values into a many2many or one2manhy relation table in odoo (former OpenERP). Do you have any idea how to do this?

Answer

Don Kirkby picture Don Kirkby · Feb 22, 2012

Here's an example from the stock module:

invoice_line_id = invoice_line_obj.create(cursor, user, {
    'name': name,
    'origin': origin,
    'invoice_id': invoice_id,
    'uos_id': uos_id,
    'product_id': move_line.product_id.id,
    'account_id': account_id,
    'price_unit': price_unit,
    'discount': discount,
    'quantity': move_line.product_uos_qty or move_line.product_qty,
    'invoice_line_tax_id': [(6, 0, tax_ids)],
    'account_analytic_id': account_analytic_id,
    }, context=context)
self._invoice_line_hook(cursor, user, move_line, invoice_line_id)

The invoice_line_tax_id field is a many-to-many relationship, and the (6, 0, tax_ids) means to replace any existing records with those in tax_ids. Because you're calling create(), there's nothing to replace.

A full list of options is in the documentation for the osv class.

For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics

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