top of page
Writer's pictureTek Siong, Hock

Odoo ORM common method - update records

Updated: Jul 8, 2022

This Odoo technical development blog is my way to contribute back to the Odoo Community, for all the selfless and great sharing by the community members.


ORM update records methods are commonly used and yet under-documented.


You may find the Odoo ORM documentation here, which, as usual, is very hard to understand. My suggestion is try to find how this ORM is used in the Odoo source code.

(0, 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, 0) removes the record of id id from the set, then deletes it (from the database). Can not be used in create().
(3, id, 0) removes the record of id from the set, but does not delete it. Can not be used in create().
(4, id, 0) adds an existing record of id id to the set.
(5, 0, 0) removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used in create().
(6, 0, 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.

Below is the example on how to update unit price in an existing invoice line.


Click "Like" at the bottom of this blog, to motivate us to continue sharing more Odoo tips.

invoices = self.env['account.invoice'].browse(self.env.context.get('vendor_bill_id'))
invoice_lines = self.env['account.invoice.line'].browse(self.env.context.get('invoice_line_id'))
# the fields to update 
inv_value = {
    'price_unit': xxxx,
}
invoice[0].write({
    'invoice_line_ids': [
        (1, invoice_line[0].id, inv_value),
    ]
})
    

This is an example how you can add a new record to an existing one2Many line.

vals = {'customer': customer}
vals['start_date_time'] = start_time
start_time_in_hours = start_time.hour + start_time.minute / 60
vals['start_time'] = start_time_in_hours
end_time = start_time + timedelta(0, rec.visit_duration * 60)
vals['end_date_time'] = end_time
end_time_in_hours = end_time.hour + end_time.minute/60
vals['end_time'] = end_time_in_hours
vals['sequence'] = count
vals['state'] = rec.state
start_time = end_time + timedelta(0, prep_time * 60)
rec.visit_customer_lines = [(0, 0, vals)]

1,234 views0 comments

Recent Posts

See All

Comments


bottom of page