from django.db.models import Count, Sum from django.shortcuts import render # Create your views here. from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet from payment.models import Payment from payment.serializers import PaymentSerializer class PaymentView(ModelViewSet): queryset = Payment.objects.all() serializer_class = PaymentSerializer pagination_class = None # 统计 {order_by:0(默认0升序,1降序)} class StatisticsView(APIView): def get(self, request): data = request.GET order_by_value = "price_sum" if 'order_by' in data: if data['order_by'] == 1: order_by_value = "-price_sum" res = Payment.objects.values('payment_type', 'year', 'month').annotate(price_sum=Sum('index_price')).order_by( order_by_value) return Response(data=res, status=status.HTTP_200_OK)