views.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django.db.models import Sum, Count
  2. from django.shortcuts import render
  3. # Create your views here.
  4. from rest_framework import permissions, status
  5. from rest_framework.decorators import permission_classes
  6. from rest_framework.response import Response
  7. from rest_framework.views import APIView
  8. from rest_framework.viewsets import ModelViewSet
  9. from pivottable.models import *
  10. from pivottable.serializer import *
  11. # @permission_classes((permissions.AllowAny,))
  12. class PaymentViews(ModelViewSet):
  13. pagination_class = None
  14. queryset = Payment.objects.all()
  15. serializer_class = PaymentSerializer
  16. # @permission_classes((permissions.AllowAny,))
  17. class MenuView(ModelViewSet):
  18. queryset = Menu.objects.all()
  19. serializer_class = MenuSerializer # 序列化
  20. pagination_class = None
  21. class PayStatViews(APIView):
  22. # 统计 {order_by:0(默认0升序,1降序)}
  23. def get(self, request):
  24. data = request.GET
  25. order_by = "price_sum"
  26. if hasattr(data, "order_by") and data['order_by'] == 1:
  27. order_by = "-price_sum"
  28. data = Payment.objects.values('payment_type',price_sum=Sum('index_price'))\
  29. .annotate(payment_type_count=Count('payment_type')).order_by(order_by)
  30. return Response(data=data, status=status.HTTP_200_OK)