Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
4 Respostas
860 Visualizações


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
Cancelar
Melhor resposta

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
Cancelar
Melhor resposta

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
Cancelar
Melhor resposta

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
Cancelar
Melhor resposta

def sc_create(self):

print(self)

self.create(

{

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

​ ​ ​})

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
0
dez. 18
3715
1
out. 23
971
1
jul. 23
2081
0
jan. 22
2745
1
jun. 23
3150