视图work
Posted hj59988326
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视图work相关的知识,希望对你有一定的参考价值。
视图集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的主要内容,如果未能解决你的问题,请参考以下文章