Ajuda

0

computed fields

Avatar
jenan soliman

Hello,

I want to calculate the staff number of departments after entering the department.

Here's my code:


class TrainingAndDevelopment(models.Model):
_name = 'hr.training'
_inherit = 'mail.thread'
_description = "training and development"

employee_name = fields.Many2one('hr.employee', string='Employee Name', required=True)
job_title = fields.Char("Job Title", required=True)
employee_status = fields.Char(string='employee status')
date = fields.Date('Date', required=True, readonly=True, default=fields\.Date\.today\(\)\)
department_id\ =\ fields\.Many2one\('hr\.department',\ string='Department',\ required=True\)
manager_name\ =\ fields\.Many2one\('hr\.employee',\ string='Manager\ Name',\ \)
No_of_Staff\ =\ fields\.Integer\(string='No\ of\ Staff',\ compute='staff_number'\)

@api\.depends\('department_id',\)
def\ staff_number\(self\):
for\ record\ in\ self:
record\.No_of_Staff\ =\ record\.env\['hr\.department'\]\.search_count\(
\[\('department_id',\ '=', record.department.id)])




Thank you in advance for any help



Avatar
Descartar
1Resposta
2
Avatar
Kiran K
Melhor Resposta

Hi ,

Try,

staff_count = self.env['hr.employee'].search_count([('department_id', '=', record.department.id)])


Hope it Helps,

Kiran K

3Comentários
Avatar
Descartar
Avatar
jenan soliman
-

I get the same error
AttributeError: 'hr.training' object has no attribute 'department'

Avatar
Kiran K
-

staff_count = self.env['hr.employee'].search_count([('department_id', '=', record.department_id.id)]

Avatar
jenan soliman
-

Thank you, It works.