This question has been flagged
5 Replies
6408 Views

Hey guys,

I am sending a post request to the res.partner model. And I want to give value to category_id which is selection field (many2many). How I can achieve it?

My code is below:

id = models.execute_kw(db, uid, password, 'res.partner', 'create', [{
'name': name, 'x_customer_register_date': my_date, 'x_customer_id': customerId, 'category_id': tags,
'zip': postcode, 'street': address,
}])



Avatar
Discard
Best Answer

Hi,

Follow the below syntax for filling the values to many2many fields.


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

Avatar
Discard
Best Answer

Hi Nazrin,

There are basically two way to add record to many2many field.

1) Add existing created record to many2many field

- Add single record:

tags = [(4, EXISTING_RECORD.id)]

'category_id': tags,

- Add multiple records:

tags = [EXISTING_RECORD1.id, EXISTING_RECORD2.id, EXISTING_RECORD3.id, EXISTING_RECORD4.id, EXISTING_RECORD5.id]

'category_id': [(4, tag, None) for tag in tags]


2) Create new record and add to many2many field


tag_vals = {
'name': "Tag-1",
}

'category_id': [(0, 0, tag_vals)]


For about how to fill value to x2many field: https://odoo-development.readthedocs.io/en/latest/dev/py/x2many.html

I hope it will helpful for you

Thanks and regards
Haresh Kansara

Avatar
Discard
Best Answer

https://odoo-development.readthedocs.io/en/latest/dev/py/x2many.html

Avatar
Discard

first read the doc and do like eg: 'x_id': [(6, 0, tax_ids)],

6,0 will replace remove existing tags add new added tags. So i suggest to use 4,0 to add

yeah thanks and i thought the person would go through the link to understand each,else they might ask how to remove on next question and so on

Many2many fields always have an intermediary table where it's stored the foreign keys of the two tables, the actual were the field is m2m defined and the pointed table of the m2m field. Using that table the records of the other table can be retrieved. The following is a definition of a many2many field with the intermediary table in bold

invoice_line_tax_id = fields.Many2many('account.tax', 'account_invoice_line_tax', 'invoice_line_id', 'tax_id', string='Taxes', domain=[('parent_id', '=', False)])

In the case of the payment_ids of the model account.invoice it's a computed field so the records of the other table are retrieved using a the function _compute_payments of the account.invoice. The computed m2m fields don't have an intermediary table

Author Best Answer

It is string in my variable, like B2B or B2C

and I want to choose accordingly, so in my case the solution above is not working

Avatar
Discard

many2many field right?

it stores ids in both side so u have to write the ids

find id of B2B and B2C and store it

Author

for small stuffs like B2B or B2C i did now like this. But there is one problem now. Now I want to do orders and connect it with customers, there is field which is one2many and I need to choose customer's id. And the number of customers are extremely big. In this case, what should I do?

Author

many2one sorry