Django Rest Framework 序列化接口使用视图组件Viewset优化(最终版优化)!
'''2.使用viewset优化后的url''' urlpatterns = [ re_path(r'book/$', views.BookView.as_view({ 'get': 'list', 'post': 'create', })), re_path(r'book/(?P<pk>\d+)/$', views.BookView.as_view({ 'get': 'retrieve', 'put': 'update', 'delete': 'destroy' })), ]
'''5.视图组最终优化:使用modelsviewset优化''' from rest_framework.viewsets import ModelViewSet class BookView(ModelViewSet): queryset = models.Book.objects.all() serializer_class = BookSerizlizer