74

from rest_framework.views import APIView
from rest_framework.response import Response
from .response import APIResponse
from . import models, serializers

class CarAPIView(APIView):
# 群查
def get(self, request, *args, **kwargs):
car_query = models.Car.objects.filter(is_delete=False).all()
car_ser = serializers.CarModelSerializer(instance=car_query, many=True)
return APIResponse(results=car_ser.data)

""" 视图家族
from rest_framework import views, mixins, generics, viewsets

views:视图类 - APIView,GenericAPIView(generics中)
mixins:视图工具类 - CreateModelMixin,ListModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
generics:工具视图类 - 九个类
viewsets:视图集 - 两个视图集基类,两个视图集子类,一个工具类
"""

回顾APIView类:1)拥有View的所有 2)重写as_view 3)重写dispatch 4)一系列类属性

GenericAPIView类

1)拥有APIView的所有

from rest_framework.generics import GenericAPIView
class CarGenericAPIView(GenericAPIView):
# 不能直接写到objects结束,因为objects结束时,不是QuerySet对象,而是Manager对象,但 .all() 和 .filter() 后一定是QuerySet对象
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer
lookup_url_kwarg = 'pk'
# 群查
"""
def get(self, request, *args, **kwargs):
# car_query = models.Car.objects.filter(is_delete=False).all()
car_query = self.get_queryset()

    # car_ser = serializers.CarModelSerializer(instance=car_query, many=True)
    car_ser = self.get_serializer(car_query, many=True)

    return APIResponse(results=car_ser.data)
"""

# 单查
def get(self, request, *args, **kwargs):
    # pk = kwargs.get('pk')
    # car_obj = models.Car.objects.filter(is_delete=False).filter(pk=pk).first()
    car_obj = self.get_object()

    # car_ser = serializers.CarModelSerializer(car_obj)
    car_ser = self.get_serializer(car_obj)

    return APIResponse(results=car_ser.data)

mixins视图工具类

from rest_framework.mixins import RetrieveModelMixin, ListModelMixin, CreateModelMixin
class CarReadCreateGenericAPIView(ListModelMixin, RetrieveModelMixin, CreateModelMixin, GenericAPIView):
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer
lookup_url_kwarg = 'pk'
# 群查
# """
def get(self, request, *args, **kwargs):
# car_query = self.get_queryset()
# car_ser = self.get_serializer(car_query, many=True)
# return APIResponse(results=car_ser.data)
return self.list(request, *args, **kwargs)
# """

# 单查
"""
def get(self, request, *args, **kwargs):
    # car_obj = self.get_object()
    # car_ser = self.get_serializer(car_obj)
    # return APIResponse(results=car_ser.data)
    response = self.retrieve(request, *args, **kwargs)
    return APIResponse(results=response.data)
"""

# 单增
def post(self, request, *args, **kwargs):
    return self.create(request, *args, **kwargs)

Generics工具视图类

单独完成单查接口

from rest_framework.generics import RetrieveAPIView
class CarRetrieveAPIView(RetrieveAPIView):
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer
lookup_url_kwarg = 'pk'

单独完成群查接口

from rest_framework.generics import ListAPIView
class CarListAPIView(ListAPIView):
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer

单查(get)、单整体改(put)、单局部改(patch)、单删接口(delete)

from rest_framework.generics import RetrieveUpdateDestroyAPIView
class CarRetrieveUpdateDestroyAPIView(RetrieveUpdateDestroyAPIView):
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer

posted @ 2019-12-30 21:32  Daoqi  阅读(754)  评论(0编辑  收藏  举报