views.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. # 统计
  15. class StatisticsView(APIView):
  16. # 统计 {order_by:0(默认0升序,1降序)}
  17. def get(self, request):
  18. data = request.GET
  19. order_by_value = "price_sum"
  20. if 'order_by' in data:
  21. if data['order_by'] == 1:
  22. order_by_value = "-price_sum"
  23. res = Payment.objects.values('payment_type', price_sum=Sum('index_price')).annotate(
  24. payment_type_count=Count('payment_type')).order_by(
  25. order_by_value)
  26. return Response(data=res, status=status.HTTP_200_OK)
  27. # 去重
  28. class groupView(APIView):
  29. # 得到指定字段去重
  30. def get(self, request):
  31. data = request.GET
  32. res = Payment.objects.values(data['field']).annotate(count=Count(data['field']))
  33. return Response(data=res, status=status.HTTP_200_OK)
  34. # queryset测试
  35. class testView(APIView):
  36. def get(self, request):
  37. data = request.GET
  38. res = Payment.objects.exclude(payment_type="一、一般公共服务").values()
  39. return Response(data=res, status=status.HTTP_200_OK)