Skip to Content
Menu
This question has been flagged
2 Replies
16152 Views

How exactly do i make a field that lets me show how many of my employees are in a certain department ? 

Here is my code 


# -*- coding: utf-8 -*-
from datetime import timedelta
from odoo import models, fields, api

class employee(models.Model):
_name = 'drs2.employee'

name = fields.Char(required=True)
age = fields.Integer()
height = fields.Integer()
annoying = fields.Boolean()

task_id = fields.One2many(
'drs2.task', 'employee_id', string="Tasks")

dep_id = fields.Many2one(
'drs2.dep', ondelete='cascade', string="Department")


class task(models.Model):
_name = 'drs2.task'

name = fields.Char(string="Task Name", required=True)
description = fields.Text()

employee_id = fields.Many2one('drs2.employee',
ondelete='cascade', string="Employee")

dep_id = fields.Many2one(
'drs2.dep', ondelete='cascade', string="Department")

start_date = fields.Date(default=fields.Date.today)

end_date = fields.Date(default=fields.Date.today)

class department(models.Model):
_name = 'drs2.dep'

name = fields.Char()

employee_id = fields.One2many(
'drs2.employee', 'dep_id', string="Employee")

count = fields.Integer(compute='count_employees')

@api.one
def count_employees(self):
return search_count(self.employee_id)


Avatar
Discard
Best Answer

Hello Leke Onile-ere,

Try below code :-

def count_employees(self):
    for employee in self:
        employee.count = self.env['drs2.employee'].search_count([('dep_id', '=', self.id)])


Hope it will works for you.

Thanks,

Avatar
Discard
Best Answer
Try this one

    @api.multi
    @api.depends('employee_id')
def count_employees(self):
return len(self.employee_id)
Avatar
Discard

works perfect.

Related Posts Replies Views Activity
2
Jul 18
2777
1
Oct 20
4033
0
Jun 19
6642
2
Sep 22
7896
2
Jul 22
7405