Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert into Many2many Odoo (former OpenERP)

I'm trying to insert values into a Many2many or One2many relation table field in Odoo (former OpenERP). Do you have any idea how to do this?

like image 315
m3asmi Avatar asked Feb 21 '12 12:02

m3asmi


2 Answers

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)

like image 158
Don Kirkby Avatar answered Sep 24 '22 02:09

Don Kirkby


def list_customers(self, cr, uid, ids, context):     sale_obj = self.pool.get('sale.order')     for sale in self.browse(cr, uid, ids, context):         sale_ids = sale_obj.search(cr, uid, [('div_code_id','=',sale.div_code_id.id),('project_user','=',sale.project_id.id),('tower_id','=',sale.tower_id.id)])         ids_cus = []         for cus in sale_obj.browse(cr, uid, sale_ids, context):             if cus.partner_id.id not in ids_cus:                 ids_cus.append(cus.partner_id.id)         self.write(cr, uid, ids, {'state_readonly':'listed','customer_ids': [(6, 0, ids_cus)]})     return True 

You can insert values into a many-to-many relation table in OpenERP, please look at above example

like image 28
MKUMAR Avatar answered Sep 23 '22 02:09

MKUMAR