Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

many to many - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...