Skip to Content
Menu
This question has been flagged
4 Replies
766 Views


class DepartmentInfo(models.Model):

_name = "department.info"

_description = "Department Details"

_rec_name = "department_name"

_inherit = ["mail.thread", "mail.activity.mixin"]


department_name = fields.Char(string="Department Name")

department_code = fields.Integer(string="Department Code")

department_hod_name = fields.Char(string="Head Of Department")

employee_ids = fields.One2many(

comodel_name="employee.info", inverse_name="department_id", string="Employees"

)


def sc_create(self):

print(self)

self.create(

{

"employee_ids": ​ [(0,0,"first_name":f"TEMP{self.id}","department_id":self,},)]

​})


the department_id of the newly created record is empty

Avatar
Discard
Best Answer

Hello Divyang Kuntar,

Please update the following code in your implementation. After applying this update, you will be able to display the department value correctly. Currently, the department is not being passed correctly.

def sc_create(self):
self.create({
"employee_ids": [(0, 0, {
"first_name": f"TEMP{self.id}",
"department_id": self.id
})]
})

Thanks & Regards,

Yahoo Baba

📞 Skype: Yahoo Baba Official

✉️ Email: yahoobaba077@gmail.com

Avatar
Discard
Best Answer

Hey Divyang,

You should use the write method to add "employee_ids" records. Use the create command for new record creation. Here is a sample for you.

# Importing Command
from odoo import Command

def sc_create(self):
​self.write({"employee_ids": [Command.create({'first_name': employee_name})]})

This will update the employee_ids field of the current (self) record. So it will automatically populate department_id to the newly created records with this method.

Thank you.

Avatar
Discard
Best Answer

I assume you want to fill the "department_id" with the current object id.

By right you should face an error instead of empty record because Odoo is trying to adapt different types - Integer vs DepartmentInfo().


It's a good practice to always add a

self.ensure_one() or loop the self to ensure you don't face singleton errors

Avatar
Discard
Best Answer

def sc_create(self):

print(self)

self.create(

{

"employee_ids": ​ ​ ​ ​ [(0,0,"first_name":f"TEMP{self.id}","department_id":self.id,},)]

​ ​ ​})

Avatar
Discard
Related Posts Replies Views Activity
0
Dec 18
3601
1
Oct 23
916
1
Jul 23
1973
0
Jan 22
2645
1
Jun 23
3048