from django.db.models import Sum, Count from django.shortcuts import render # Create your views here. from rest_framework import permissions, status from rest_framework.decorators import permission_classes from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet from pivottable.models import * from pivottable.serializer import * # @permission_classes((permissions.AllowAny,)) class PaymentViews(ModelViewSet): pagination_class = None queryset = Payment.objects.all() serializer_class = PaymentSerializer # @permission_classes((permissions.AllowAny,)) class MenuView(ModelViewSet): queryset = Menu.objects.all() serializer_class = MenuSerializer # 序列化 pagination_class = None class PayStatViews(APIView): # 统计 {order_by:0(默认0升序,1降序)} def get(self, request): data = request.GET order_by = "price_sum" if hasattr(data, "order_by") and data['order_by'] == 1: order_by = "-price_sum" data = Payment.objects.values('payment_type',price_sum=Sum('index_price'))\ .annotate(payment_type_count=Count('payment_type')).order_by(order_by) return Response(data=data, status=status.HTTP_200_OK)