28

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?

Salek
  • 449
  • 1
  • 10
  • 19
m3asmi
  • 1,252
  • 1
  • 16
  • 36

4 Answers4

54

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)

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • But i don't know the object name of the many2many rel table... That means i can view that two-col-table in database, but i don't know how to get it through http.request.registry[]. Do you know how? – Summer Sun Apr 21 '16 at 09:30
7
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

MKUMAR
  • 71
  • 1
  • 1
3

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)
Stephan
  • 41,764
  • 65
  • 238
  • 329
Anup
  • 200
  • 1
  • 13
0

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

Sudhir Arya
  • 3,703
  • 3
  • 30
  • 43
  • can we add list to vals parameter in 'create(self, cr, user, vals, context=None):' for insert multiple records at once.? – Priyan RockZ Nov 04 '13 at 09:42