Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add , update and delete Many2many field records in odoo 10?

Tags:

python

odoo

I am setting up function in a wizard which will do following actions:

  1. Add new record and linked to current existing Many2many Field.
  2. Update record of current existing Many2many Field.
  3. Delete current existing Many2many Field. Wizard model and Actual model having two Many2many fields
  4. customers_ids = fields.Many2many('res.partners', 'Customers')
  5. new_customers_ids = fields.Many2many('res.partners', 'New Customers')

In View Customers_ids are read only view where as new_customers_ids allowing add item (customer) and delete.

When I am adding new customers new_customers_ids from view but can't now updating customers_ids(Customers ids) via clicking button save on wizard. How can add/ delete and update records from customers_ids via adding/ deleting and updating in new_customers_ids ?

 @api.multi 
def applychanges(self):

    for record in self:
        customers = []
        new_customers = []
        for customer in record.customers_ids:
            customers.append(customer.id)
        customers = list(set(customers))

        for x in record.new_customers_ids:
            new_customers.append(x.id)
        new_customers = list(set(new_customers_ids))
    
        record.customers_ids = [(1, 0, new_customers)]
       
like image 274
majid Avatar asked Oct 25 '25 15:10

majid


1 Answers

Per the ORM documentation, using a left operator of 1 should be used as:

(1, id, values)

And it effectively

Updates an existing record of id id with the values in values.


In your code, you are using (1, 0, values) which is trying to update the record of id == 0, which can't possibly exist.

For what it's worth, I've rarely seen the left operator used as 1. Usually, I use 4 to update the record value, which will add the new_customer to the record's customer_ids field:

record.customers_ids = [(4, 0, new_customers[0])]

However, using 4 only supports adding one record at a time (which is why I used new_customers[0] in the above example. If you want to add many at once, you can use list comprehension:

record.customers_ids = [(4, 0, new_customer) for new_customer in new_customers]

For completeness, here is the snippet from the documentation with the purpose and syntax of each possible left operator. For reference, I've almost only ever used 3, 4, or 6.

(0, _, values)

Adds a new record created from the provided value dict.

(1, id, values)

Updates an existing record of id id with the values in values. Can not be used in create().

(2, id, _)

Removes the record of id id from the set, then deletes it (from the database). Can not be used in create().

(3, id, _)

Removes the record of id id from the set, but does not delete it. Can not be used on One2many. Can not be used in create().

(4, id, _)

Adds an existing record of id id to the set. Can not be used on One2many.

(5, _, _)

Removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().

(6, _, ids)

Replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.

like image 132
travisw Avatar answered Oct 28 '25 05:10

travisw