Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
4 Odgovori
839 Prikazi


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
Opusti
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
Opusti
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
Opusti
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
Opusti
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
Opusti
Related Posts Odgovori Prikazi Aktivnost
0
dec. 18
3701
1
okt. 23
967
1
jul. 23
2066
0
jan. 22
2724
1
jun. 23
3126