Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to use compute with One2many?

Jean-Baptiste Bernard
on 6/26/15, 10:29 AM 3,779 views

Hello I want to know how to use compute to fill a One2Many



| 6 4 7
Tbilisi, Georgia
On 6/27/15, 5:28 AM

option with recordsets:

o2m_field = fields.One2many(....,compute="_compute_o2m_field")

def _compute_o2m_field(self):
### get recordset of related object, for example with search (or whatever you like):
related_recordset = self.env["the.relation.obj"].search([("some", "condition","here")])
self.o2m_field = related_recordset

option with ids:

o2m_field = fields.One2many(....,compute="_compute_o2m_field")

def _compute_o2m_field(self): related_ids = [] # here compute & fill related_ids with ids of related object self.o2m_field.ids = related_ids

together with @api.one add @api.depends(...) as well, if your calculation of related recordset or ids depends on another fields in a model.

Thank you i going to try this but if i want to create record in my on2many it's possible?

Aldébarande, Jean-Baptiste Bernard
on 6/29/15, 8:30 AM

you mean make this field editable? in this case you'll have to implement another function as well, the "inverse" function, see doc: https://www.odoo.com/documentation/8.0/reference/orm.html#field-computed

on 6/29/15, 8:40 AM

Not exactly in my one2many, i want to fill a result for eg : timesheet_ids = timesheet_obj.search([('employee_id','=', self.id)]) for timesheet_id in timesheet_ids : time_geh[timesheet_id.machine_id.geh_id.id] += timesheet_id.time_amount for geh_id_int in time_geh : geh_id = geh_obj.browse(geh_id_int) my_ids = my_one2many.create({'name':geh_id.name, 'total_time' : time_geh[geh_id]}) self.my_one2many.ids = my_ids

Aldébarande, Jean-Baptiste Bernard
on 6/29/15, 10:35 AM

yes, technically it's possible, but I do not see how it may be useful to create records from inside of compute function... as compute function is called several times, each time when you access the field, when field displayed in UI, etc... so new records will be created over and over...

on 6/29/15, 3:07 PM

Yeah i understund this is why i try to do each time to make an unlink to empty the table but i have keyerror : 1

Aldébarande, Jean-Baptiste Bernard
on 6/30/15, 3:00 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 6/26/15, 10:29 AM
Seen: 3779 times
Last updated: 9/2/15, 11:22 PM