12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 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
- # 统计
- class StatisticsView(APIView):
- # 统计 {order_by:0(默认0升序,1降序)}
- 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', price_sum=Sum('index_price')).annotate(
- payment_type_count=Count('payment_type')).order_by(
- order_by_value)
- return Response(data=res, status=status.HTTP_200_OK)
- # 去重
- class groupView(APIView):
- # 得到指定字段去重
- def get(self, request):
- data = request.GET
- res = Payment.objects.values(data['field']).annotate(count=Count(data['field']))
- return Response(data=res, status=status.HTTP_200_OK)
- # queryset测试
- class testView(APIView):
- def get(self, request):
- data = request.GET
- res = Payment.objects.exclude(payment_type="一、一般公共服务").values()
- return Response(data=res, status=status.HTTP_200_OK)
|