views.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. from django.db.models import Count, Sum
  2. from django.shortcuts import render
  3. # Create your views here.
  4. from rest_framework import status
  5. from rest_framework.response import Response
  6. from rest_framework.views import APIView
  7. from rest_framework.viewsets import ModelViewSet
  8. from payment.models import Payment
  9. from payment.serializers import PaymentSerializer
  10. class PaymentView(ModelViewSet):
  11. queryset = Payment.objects.all()
  12. serializer_class = PaymentSerializer
  13. pagination_class = None
  14. # 统计 {order_by:0(默认0升序,1降序)}
  15. class StatisticsView(APIView):
  16. def get(self, request):
  17. data = request.GET
  18. order_by_value = "price_sum"
  19. if 'order_by' in data:
  20. if data['order_by'] == 1:
  21. order_by_value = "-price_sum"
  22. res = Payment.objects.values('payment_type', 'year', 'month').annotate(price_sum=Sum('index_price')).order_by(
  23. order_by_value)
  24. return Response(data=res, status=status.HTTP_200_OK)