Question

I am writing a django project and trying to find each employee's total commissions.

my sale model:

class Sale(models.Model):

commission=models.DecimalField(max_digits=7,decimal_places=2,blank=True,null=True)
ouremployee=models.ForeignKey(Employee,blank=True,null=True)

My sale object already recored commission associated with each sale and each employee but now I want to find each employee's total commission.

my employee model:

class Employee(models.Model):
    Name=models.CharField(max_length=30,blank=True,null=True)
Hire_date=models.DateTimeField(blank=True,null=True)
Salary=models.DecimalField(max_digits=8,decimal_places=1,blank=True,null=True)
Commission_rate=models.DecimalField(default=0,max_digits=3,decimal_places=3,blank=True,null=True)
Title=models.CharField(max_length=30,blank=True,null=True)

my views.py:

Sale = Sale.objects.all()
total_commission=0
TotalCommission = Sale.annotate(total_commission=Sum('ouremployee'))

My views.py doesnot work. May anyone help me .

Was it helpful?

Solution

Follow the relationship backwards:

Employee.objects.annotate(total_commission=Sum('sale__commission'))

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top