I am setting up function in a wizard which will do following actions:
Many2many Field.Many2many Field.Many2many Field.
Wizard model and Actual model having two Many2many fieldscustomers_ids = fields.Many2many('res.partners', 'Customers')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)]
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
idwith 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
idwith the values invalues. Can not be used increate().(2, id, _)
Removes the record of id
idfrom the set, then deletes it (from the database). Can not be used increate().(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 increate().(4, id, _)
Adds an existing record of id
idto the set. Can not be used onOne2many.(5, _, _)
Removes all records from the set, equivalent to using the command
3on every record explicitly. Can not be used onOne2many. Can not be used increate().(6, _, ids)
Replaces all existing records in the set by the
idslist, equivalent to using the command5followed by a command4for eachidinids.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With