此时,我们有两个类视图,ProjectView
、ProjectDetailView
,我们来看看有啥区别,一个是没有传id的,一个是传了id的,但是两个类视图中的queryset和serializer_class都是一样的,那么我们有没有什么办法只写一个类视图呢?有,使用GenericViewSet。
GenericViewSet
- 继承ViewSetMixin和GenericAPIView
- ViewSetMixin具备给as_view传递字典参数,key为请求方法名称(get、post、put、delete),value为需要调用的action方法名称。
- GenericAPIView提供get_queryset,get_serializer、get_object等,支持过滤、排序和分页的操作
打开**project\views.py**
,注释**ProjectView**
和**ProjectDetailVie**
类,新增ProjectViewSet类,修改代码如下
from .models import Projects
from .serializers import ProjectsModelSerializer
from rest_framework import filters, viewsets, mixins
class ProjectViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin, viewsets.GenericViewSet):
# 一般需要指定queryset、serializer_class类属性
queryset = Projects.objects.all()
serializer_class = ProjectsModelSerializer
# 指定过滤引擎
filter_backends = [filters.SearchFilter, filters.OrderingFilter]
# 过滤的字段
search_fields = ['name', 'desc']
ordering_fields = ['name']
修改路由
既然我们不用ProjectView
、ProjectDetailView
,改用ProjectViewSet
,那么我们的路由也得改变
修改**caseplatform\url.py**
urlpatterns = [
path('admin/', admin.site.urls),
path('projects/<int:pk>/', views.ProjectViewSet.as_view()),
path('projects/', views.ProjectViewSet.as_view()),
]
修改后,发现报如下错误,我们来看看是什么意思,大概是说如果我们使用ViewSet,那么调as_view()时必须传一个参数,可以修改为.as_view({‘get’: ‘list’})
继续修改**caseplatform\urls.py**
from django.contrib import admin
from django.urls import path
from project import views
urlpatterns = [
path('admin/', admin.site.urls),
path('projects/<int:pk>/', views.ProjectViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'delete': 'destroy'
})),
path('projects/', views.ProjectViewSet.as_view({
'get': 'list',
'post': 'create'
})),
]
新增应用路由
我们现在只有一个project应用,如果应用很多,所有的路由都写在caseplatform\urls.py
里面,那么会比较难管理,所以我们一般会在每个应用下面新增urls.py
文件
新增project\urls.py
文件
from django.urls import path
from project import views
urlpatterns = [
path('projects/<int:pk>/', views.ProjectViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'delete': 'destroy'
})),
path('projects/', views.ProjectViewSet.as_view({
'get': 'list',
'post': 'create'
})),
]
修改caseplatform\urls.py
文件
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('project.urls')),
]
SimpleRouter优化路由
修改**project\urls.py**
from project import views
from rest_framework import routers
router = routers.SimpleRouter()
router.register('projects', views.ProjectViewSet)
urlpatterns = router.urls
ModelViewSet
- 继承mixins.CreateModelMixin、mixins.RetrieveModelMixin、mixins.UpdateModelMixin、mixins.DestroyModelMixin、mixins.ListModelMixin和GenericViewSet
打开**project\views.py**
,注释**ProjectView**
和**ProjectDetailVie**
类,新增ProjectViewSet类,修改代码如下
from .models import Projects
from .serializers import ProjectsModelSerializer
from rest_framework import filters, viewsets
class ProjectViewSet(viewsets.ModelViewSet):
# 一般需要指定queryset、serializer_class类属性
queryset = Projects.objects.all()
serializer_class = ProjectsModelSerializer
# 指定过滤引擎
filter_backends = [filters.SearchFilter, filters.OrderingFilter]
# 过滤的字段
search_fields = ['name', 'desc']
ordering_fields = ['name']
ok,我们继续用postman测试下。测试过程省略。