Odoo Help

Welcome!

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.

0

How to work with compute field for one2many in v8 API ?

By
Sairam Bandaru
on 12/7/15, 6:12 AM 1,985 views

I created one2many  in the hr.employee object like this

hr_leave_history_ids = fields.One2many('hr.leave.history','employee_id','Paid Leave History',compute='_compute_o2m_field')

in method _compute_o2m_field updating the field values like this self.hr_leave_history_ids = [(0,0,values)]

here values is a dictionary consists of 'hr.leave.history' object fields and values 
but its not updating in the Hr form and not even creating the records in 'hr.leave.history' table

plz help me out

1

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 12/7/15, 6:40 AM

Wrong assignment 

self.hr_leave_history_ids = [(0,0,values)]
here the field hr_leave_history_ids is a recordset of 'hr.leave.history' object (as it's relational field in v8 style), but you assigning a raw list to it.
If you're adding relation to a single object, then it should be like:

self.hr_leave_history_ids  |=  self.env['hr.leave.history'].create(values)

as your field type is One2many, potentially there may be need to add multiple records, in such a case you can collect them into your field with the same "|"  operator: 

for values in values_list:
    self.hr_leave_history_ids |= self.env['hr.leave.history'].create(values)

note the "|" in "|=" operator, it's one of the allowed "Set operations". you can use other set operations as well, if applicable to your case (see documentation here).  Here is relevant quote:

  • set1 | set2 returns the union of the two recordsets, a new recordset containing all records present in either source

NOTE:
create() method used as an example here, however it does not matter how you acquire a recordset you assign to relational field in it's  compute function, either create(), search() or browse() or whatever method yields recordset required may work.

Thanks a lot.Its working

Sairam Bandaru
on 12/7/15, 7:14 AM

You're welcome!

Temur
on 12/7/15, 7:17 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)

Stats

Asked: 12/7/15, 6:12 AM
Seen: 1985 times
Last updated: 12/7/15, 7:14 AM