Skip to Content
Menu
This question has been flagged
1 Reply
1151 Views

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
Discard
Best Answer

Hi ,

Try,

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


Hope it Helps,

Kiran K

Avatar
Discard
Author

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

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

Author

Thank you, It works.

Related Posts Replies Views Activity
3
Nov 23
15301
3
Nov 24
21043
1
Apr 23
4559
2
Dec 22
5494
1
Nov 22
2658