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?
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
Just put your many2many field in view (xml file) and after running your module you can see many2many field to insert records in your gui
When we create the many2many field then we used this syntax:
'field_name':fields.many2many('Module_name','relation_name','self_id','module_name_id','string',
Now u need to insert into this relation by execute queries like:
$ cr.execute('insert into relation_name (self_id,module_name_id) values(%s,%s)',(first_value,second_value)
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)