Django聚合 - 查找每个员工的总佣金

时间:2014-03-08 03:53:24

标签: python database django sum aggregation

我正在写一个django项目并试图找到每位员工的总佣金。

我的销售模式:

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)

我的销售对象已经记录了与每笔销售和每位员工相关的佣金,但现在我想找到每位员工的总佣金。

我的员工模特:

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'))

我的views.py不起作用。愿任何人帮助我。

1 个答案:

答案 0 :(得分:1)

向后追寻关系:

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

希望有所帮助。

相关问题