models.py
class QaCommission(models.Model):
user = models.ForeignKey(PlUser, on_delete=models.CASCADE, blank=True, null=True, related_name='user_commission')
ref = models.ForeignKey(PlUser, on_delete=models.CASCADE, blank=True, null=True, related_name='ref_commission')
price = models.FloatField(blank=True, null=True)
pct = models.FloatField(blank=True, null=True)
commission = models.FloatField(blank=True, null=True)
status = models.IntegerField(blank=True, null=True, default=0)
serializers.py
class QaCommissionSerializer(serializers.ModelSerializer):
class Meta:
model = QaCommission
fields = '__all__'
views.py
class QaCommissionList(viewsets.ModelViewSet):
queryset = QaCommission.objects.all()
serializer_class = QaCommissionSerializer
If we are filtering ref=60 in this view, results show like this:
{
"count": 18,
"next": "http://127.0.0.1:8008/api/qacommission/?ref=60&page=2",
"previous": null,
"results": [
{
"id": 1,
"price": 20.0,
"pct": 0.1,
"commission": 2.0,
"status": 1,
"user": 7,
"ref": 60
},
{
"id": 2,
"price": 10.0,
"pct": 0.1,
"commission": 1.0,
"status": 1,
"user": 7,
"ref": 60
},
......
......
......
{
"id": 10,
"price": 15.0,
"pct": 0.1,
"commission": 1.5,
"status": 1,
"user": 7,
"ref": 60
}
]
}
I wanna sum up all the "commission" field in the results and attach the sum to the original queryset (maybe next to "count":18), as shown above, there are 18 commission need to be count.
How could I implement this? Need your help, thanks!