Se rendre au contenu
Menu
Cette question a été signalée
4 Réponses
795 Vues


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
Ignorer
Meilleure réponse

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
Ignorer
Meilleure réponse

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
Ignorer
Meilleure réponse

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
Ignorer
Meilleure réponse

def sc_create(self):

print(self)

self.create(

{

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

​ ​ ​})

Avatar
Ignorer
Publications associées Réponses Vues Activité
0
déc. 18
3618
1
oct. 23
932
1
juil. 23
1999
0
janv. 22
2672
1
juin 23
3082