day75

题目:

1、整理今天所学知识点

2、查看并研究ListAPIView工具视图类,继承该类完成汽车资源的群查接口,汽车资源需要有image资源,查看接口image资源的值

3、研究GenericViewSet工具集,继承该类完成汽车资源的单查,群查,单增,单改,单删接口

urls.py


from . import views
from django.conf.urls import url

urlpatterns = [
    
    url(r'^v2/Cars/$', views.CarViewList.as_view()),

    url(r'^v3/Cars/$', views.CarViewGenericViewSet.as_view({'get': 'list'})),
    url(r'^v3/Cars/(?P<pk>\d+)$', views.CarViewGenericViewSet.as_view(
        {'get': 'retrieve', 'post': 'create', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
]

views.py


class CarViewList(ListAPIView):
    serializer_class = serializers.CarModelSrealizer
    queryset = models.Car.objects.filter(is_delete=False).all()
    lookup_url_kwarg = 'pk'

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)


class CarViewGenericViewSet(GenericViewSet, ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin,
                            DestroyModelMixin):
    serializer_class = serializers.CarModelSrealizer
    queryset = models.Car.objects.filter(is_delete=False).all()
    lookup_url_kwarg = 'pk'

posted @ 2019-12-30 20:57  W文敏W  阅读(106)  评论(0编辑  收藏  举报