码迷,mamicode.com
首页 > 其他好文 > 详细

视图work

时间:2019-12-30 22:49:25      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:work   pat   from   render   list   res   except   any   post   

视图集work

'''
views.py
'''
from django.shortcuts import render
from api import models
# Create your views here.


from rest_framework.views import APIView
from rest_framework.response import Response
from api import response,serializer
from rest_framework.generics import ListAPIView
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin

from rest_framework.viewsets import GenericViewSet,ModelViewSet
# class CarsApiView(ListAPIView):
#     queryset = models.Car.objects.filter(is_delete=False).all()
#     serializer_class = serializer.CarModelSerializer

class CarsApiView(ModelViewSet,APIView):
    queryset = models.Car.objects.filter(is_delete=False).all()
    serializer_class = serializer.CarModelSerializer
    def many_update(self, request, *args, **kwargs):
        request_data = request.data
        print(request.data)
        try:
            pks = []
            for dic in request_data:
                pk = dic.pop('pk')  
                pks.append(pk)

            book_query = models.Car.objects.filter(is_delete=False, pk__in=pks).all()
            if len(pks) != len(book_query):
                raise Exception('pk对应的数据不存在')
        except Exception as e:
            return Response({'detail': '%s' % e}, status=400)

        book_ser = serializer.CarModelSerializer(instance=book_query, data=request_data, many=True)

        book_ser.is_valid(raise_exception=True)
        book_list = book_ser.save()
        print(123,)
        return response.APIResponse(results=serializer.CarModelSerializer(book_list, many=True).data)

    def many_partial_update(self, request, *args, **kwargs):
        request_data = request.data
        try:
            pks = []
            for dic in request_data:
                pk = dic.pop('pk')
                pks.append(pk)

            book_query = models.Car.objects.filter(is_delete=False, pk__in=pks).all()
            if len(pks) != len(book_query):
                raise Exception('pk对应的数据不存在')
        except Exception as e:
            return Response({'detail': '%s' % e}, status=400)

        book_ser = serializer.CarModelSerializer(instance=book_query, data=request_data, many=True, partial=True)
        book_ser.is_valid(raise_exception=True)
        book_list = book_ser.save()
        return response.APIResponse(results=serializer.CarModelSerializer(book_list, many=True).data)
    def many_destroy(self, request, *args, **kwargs):
        pks = request.data
        try:
            rows = models.Car.objects.filter(is_delete=False, pk__in=pks).update(is_delete=True)
        except:
            return response.APIResponse(status=1,msg='数据有误')
        if rows:
            return response.APIResponse(status=0,msg='删除成功')

    def create(self, request, *args, **kwargs):
        print(request.data)
        if isinstance(request.data,list):
            car_ser = serializer.CarModelSerializer(data=request.data, many=True)
            car_ser.is_valid(raise_exception=True)
            car_obj_or_list = car_ser.save()
            return response.APIResponse(results=serializer.CarModelSerializer(car_obj_or_list, many=True).data)

        else:
            return Response(data={
                'detail': '数据有误'
            }, status=400)

'''
urls.py
'''
urlpatterns = [
    url('^cars/$',views.CarsApiView.as_view({
        'get':'list',
        'post':'create',
        'put':'many_update',
        'patch':'many_partial_update',
        'delete':'many_destroy'
    })),
    url('^cars/(?P<pk>\d+)$',views.CarsApiView.as_view({
        'get':'retrieve',
        'put':'update',
        'patch':'partial_update',
        'delete':'destroy'
    }
    )),

]

视图work

标签:work   pat   from   render   list   res   except   any   post   

原文地址:https://www.cnblogs.com/hj59988326/p/12122022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!