Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
4 Antworten
832 Ansichten


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
Verwerfen
Beste Antwort

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
Verwerfen
Beste Antwort

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
Verwerfen
Beste Antwort

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
Verwerfen
Beste Antwort

def sc_create(self):

print(self)

self.create(

{

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

​ ​ ​})

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
0
Dez. 18
3686
1
Okt. 23
966
1
Juli 23
2042
0
Jan. 22
2721
1
Juni 23
3124