我有一个类Transaction:
class Transaction(models.Model):
Driver = models.ForeignKey(User,related_name='Driver')
Passenger = models.ForeignKey(User,related_name='Passenger')
DriverRoute = models.ForeignKey(ServiceProviderRouteDetails)
PassengerRoute = models.ForeignKey(PassengerRouteDetails)
Status = models.CharField(max_length=20)
然后我有一个TransactionHistory类:
class TransactionHistory(models.Model):
Transaction = models.ForeignKey(Transcation)
User = models.ForeignKey(User)
Debit = models.FloatField(null='true')
Credit = models.FloatField(null='true')
Status = models.CharField(max_length=25)
DateOfPayment = models.DateTimeField(default=now)
事务历史记录将Transaction列作为指向Transaction表的外键。 我的问题是我想计算登录用户的每个驾驶员路线的借方总和。对于DriverRoute,事务表可以有多行。
我想做这样的事情:
TranscationHistory.objects.values('Transcation.DriverRoute.id').filter(User_id=request.user.id,Status='Completed').annotate(sum_score=Sum('Credit'))
但它没有用......有什么猜测吗?
答案 0 :(得分:0)
使用__
跟踪django查询中的关系。这对你有用......
TranscationHistory.objects.values('transaction__driverroute__id')
.filter(User__id=request.user.id,Status='Completed')
.annotate(sum_score=Sum('Credit'))