Python 字段值总和的最大值
我有一个模型发票。下面是一个缩小版Python 字段值总和的最大值,python,django,postgresql,Python,Django,Postgresql,我有一个模型发票。下面是一个缩小版 class Invoice(models.Model): appointment = models.JSONField(blank=False, null=True) doctor_amount = models.FloatField(null=True, blank=True, default=0.00) “预约”字段包含医生和患者: "appointment": { "id"
class Invoice(models.Model):
appointment = models.JSONField(blank=False, null=True)
doctor_amount = models.FloatField(null=True, blank=True, default=0.00)
“预约”字段包含医生和患者:
"appointment": {
"id": 14,
"doctor": {
"id": 103,
"name": "MATTHEW"},
"patient": {
"id": 10,
"name": "JAMES"}
}
医生金额属于医生。我希望使用SQL查询来获取属于每个医生的医生金额的总和,然后从所有发票中获取具有最大医生金额总和的医生。我已经尝试了很多解决方案,但我认为两者都不太接近。所以我会留下来,希望能有新的想法
Views.py
class TopProvider(APIView):
def get(self, request, format=None):
all_invoices = Invoice.objects.all()
invoices_serializer = InvoiceSerializer(all_invoices, many=True)
invoices = invoices_serializer.data
try:
result = {}
return Response(top_doc, status=status.HTTP_200_OK)
except Exception as e:
error = getattr(e, "message", repr(e))
result["errors"] = error
result["status"] = "error"
return Response(result, status=status.HTTP_400_BAD_REQUEST)
为什么不使用聚合函数呢?