标签:work pat from render list res except any post
'''
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 pat from render list res except any post
原文地址:https://www.cnblogs.com/hj59988326/p/12122022.html