1

I am learning django or a month now. I created a model called "Leave" to mark employees leave. Then I created a model called "Salarie".In this I need to create a field like "Total_Leave" which will show an employees leave count in a month.( In january 2020 how many leave a particular employee took) ( If i mention the "Employee_Name" as "Samantha" in salary model, it need to show samantas' leave in the particular month,year)

I tried to do it like this and tried some coding but nothing worked.

@property
def Total_Leave(self):
    return self.

Can anyone explain me how to do that please?

Models.py

class Leave(models.Model):
    Leave_ID = models.CharField(max_length=5, primary_key=True,default=" ")
    Employee_Name = models.ForeignKey('Employee_Detail',models.DO_NOTHING)
    Dept_Name = models.ForeignKey('Department', models.DO_NOTHING)
    l = (
        ("Paid","Paid"),("Non-Paid","Non-Paid")
    ) 
    Leave_Type = models.CharField(max_length=10, choices= l, default="Non-Paid")
    m = (
        ("January","January"),("February","February"),("March","March"),("April","April"),("May","May"),("June","June"),("July","July"),("August","August"),("September","September"),("October","October"),("November","November"),("December","december")
    )
    Month = models.CharField(max_length=10, choices= m)
    Year = models.IntegerField(max_length=4)
    Start_Date = models.DateField()
    End_Date = models.DateField(null=True, blank = True)
    Reason = models.CharField(max_length=200)
    s = (
       ("Accepted","Accepted"),("Pending","Pending"),("Canceled","Canceled")
    )
    Status = models.CharField(max_length=10, choices= s, default="Pending")


    def __str__(self):
        return str(self.Employee_Name)

class Salarie(models.Model):
    Salarie_ID = models.CharField(max_length=5,primary_key=True,default=" ")
    Employee_Name = models.ForeignKey('Employee_Detail', models.DO_NOTHING)
    Dept_Name = models.ForeignKey('Department', models.DO_NOTHING)
    Basic_Salary = models.IntegerField(default=0)
    Extra_Hours = models.IntegerField(default=0)

    @property
    def Extra_Payment(self):
        return self.Extra_Hours * 350

    Bonus = models.IntegerField(default=0)
    @property
    def Total_Payment(self):
        return self.Extra_Payment + self.Basic_Salary + self.Bonus

    m = (
        ("January","January"),("February","February"),("March","March"),("April","April"),("May","May"),("June","June"),("July","July"),("August","August"),("September","September"),("October","October"),("November","November"),("December","december")
    )
    Month = models.CharField(max_length=10, choices= m)
    Year = models.IntegerField(max_length=4)
    Status = models.BooleanField()
    Paid_Date = models.DateField(null=True,blank=True)

    def __str__(self):
        return str(self.Employee_Name)



class Employee_Detail(models.Model):
    Employee_ID = models.CharField(primary_key=True, max_length=6)
    Employee_Name = models.CharField(unique=True, max_length=30)
    Primary_Phone = models.IntegerField(unique=True, max_length=10)
    p = (
        ("Manager","Manager"),("Supervisor","Supervisor"),("Employee","Employee")
    )
    Position = models.CharField(max_length=15, choices= p, default="Employee")
    Address = models.TextField(max_length=500)
    Email = models.EmailField(max_length=50, unique=True)

    def __str__(self):
        return str(self.Employee_Name)
Deepika
  • 737
  • 7
  • 23

2 Answers2

2

You can do this by using annotate or aggregate

If you want leave count for all the employees then

from django.db.models import Count
employees = Employee_Detail.objects.filter(leave__Status='Accepted').annotate(leave_count=Count('leave'))

you can access this by <employee Obj>.leave_count If you want leave count for all employee order_by Month then

employees = Employee_Detail.objects.filter(leave__Status='Accepted').order_by('leave__Month').annotate(leave_count=Count('leave')).values('leave_Month','leave_count')

And last you want leave count for a particular employee for a particular month than in your employee model write @property function like this

@property
def get_leave_count(self):
    leaves = Employee_Detail.objects.filter(id=self.id,leave__Status='Accepted').aggregate(leave_count=Count('leave'))
    return leaves['leave_count']

for month and year wise provide month and year and then

month = 'January' #For example    
leaves = Employee_Detail.objects.filter(id=employee_id,leave__Month=month,leave__Status='Accepted').aggregate(leave_count=Count('leave'))
#{'leave_count':3}

year = '2020' #For example    
leaves = Employee_Detail.objects.filter(id=employee_id,leave__Year=year,leave__Status='Accepted').aggregate(leave_count=Count('leave'))
#{'leave_count':13}
Pruthvi Barot
  • 1,948
  • 3
  • 14
  • Sir, I want to add @property function only.Do I need to do the previous coding also? – Deepika Jul 28 '20 at 08:55
  • 1
    No, you don't need that but in property function, you can't pass parameter like month and year it will give you result including all month and year value – Pruthvi Barot Jul 28 '20 at 09:06
  • 1
    If you want to pass month and year as well for filtering then remove @property decorator – Pruthvi Barot Jul 28 '20 at 09:09
  • Sir, I did the @property function as you told and then I add leave for an employee. But still Get_leave_count is showing as empty. I am a beginner , can you explain some more deeply – Deepika Jul 28 '20 at 09:27
  • check after filtering you are getting records or not – Pruthvi Barot Jul 28 '20 at 09:28
  • Check for filtering criteria values, like have you changed Status to 'Accepted'? – Pruthvi Barot Jul 28 '20 at 09:30
  • I found it sir... I need to change it as Employee_Name=self.Employee_Name. Now it is showing total count. But how to change it like this sir->(In 2020 july how many leave one employee have taken) – Deepika Jul 28 '20 at 09:37
  • Are you providing month and year values from view or it's static – Pruthvi Barot Jul 28 '20 at 09:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218745/discussion-between-deepika-and-pruthvi-barot). – Deepika Jul 28 '20 at 09:48
  • @PruthviBarot sir how can I get leaves count for today's date. (For eg: how many employees have taken leave today ?) – Aakash Bhaikatti Feb 03 '22 at 08:10
0

I tried something like this, maybe of some use.

def total_leave(emp_name,month,year):
    leaves = Leave.objects.filter(Emp_name= emp_name,Month=month,Year=year)
    Count=0
    for leave in leaves:
        if leaves.status== "Accepted":
            Count +=1
  return Count

Like if you only need to get leave count for the year, just use year as a parameter for function.